I have huge amount of code in a text file, for example:
"4EF\"]
,\"blue-apple\^&**%
"4EF\"]
,\"orange\/^4^&**%
How can I extract the following data:
blue-apple
orange
The data is between 4EF\"]
,\"
and \
as you can see.
You could use preg_match_all()
to get the part of the string you want:
$str = '"4EF\"]
,\"blue-apple\^&**%
"4EF\"]
,\"orange\/^4^&**%';
$str = preg_match_all('~^"4EF\\\\"[^"]+"([^\\\\]+)~m', $str, $matches);
print_r($matches[1]);
The regular expression will skip "4EF\"
+ all after the next "
, then, use a capture group to keep all until the next backslash.
Or:
$str = '"4EF\"]
,\"blue-apple\^&**%
"4EF\"]
,\"orange\/^4^&**%';
$str = preg_match_all('~^"4EF\\\\"\]\\\
,\\\\"([^\\\\]+)~m', $str, $matches);
print_r($matches[1]);
Outputs:
Array
(
[0] => blue-apple
[1] => orange
)
The regular expression:
~ # delimiter
^ # indicate that the line begins by the following
"4EF # sequence ("4EF)
\\\\ # a backslash
" # a double quote
\] # ']' need to be escaped
\\\\ # a backslash
n, # sequence (n,)
\\\\ # backslash
" # double quote
( # start capture group
[^\\\\]+ # all characters until a backslash
) # end capture group
~ # end delimiter
m # multi line (to use ^)