Regex to Capture Text Between Quotes Ver 2
This code is the continuation of my previous Regex to Capture Text Between Quotes. In this code, I just add little addition to capture text that resides between quotes(‘) or double quotes(“) even it has new line character (\n).
The main regex isĀ /(?<!\\\)”(.*?)(?<!\\\)”|(?<!\\\)”(.*\n.*?)(?<!\\\)”|(?<!\\\)\’(.*?)(?<!\\\)\’|(?<!\\\)\’(.*\n.*?)(?<!\\\)\’/i
Then the php code is:
<br />
$str = 'asdadd"1231<br />
23\""asd@asda\"\""\"\"\""d@sdf "asda\"sd"'."asdadd'1231<br />
23\''asd@asda\'\''\'\'\''d@sdf 'asda\'sd'";<br />
preg_match_all('/(?<!\\\)"(.*?)(?<!\\\)"|(?<!\\\)"(.*\n.*?)(?<!\\\)"|(?<!\\\)\'(.*?)(?<!\\\)\'|(?<!\\\)\'(.*\n.*?)(?<!\\\)\'/i', $str, $matches);print_r($matches);<br />
The code will give result like:
Array
(
[0] => Array
(
[0] => "1231
23\""
[1] => "\"\"\""
[2] => "asda\"sd"
[3] => '1231
23\''
[4] => '\'\'\''
[5] => 'asda\'sd'
)
[1] => Array
(
[0] =>
[1] => \"\"\"
[2] => asda\"sd
[3] =>
[4] =>
[5] =>
)
[2] => Array
(
[0] => 1231
23\"
[1] =>
[2] =>
[3] =>
[4] =>
[5] =>
)
[3] => Array
(
[0] =>
[1] =>
[2] =>
[3] =>
[4] => \'\'\'
[5] => asda\'sd
)
[4] => Array
(
[0] =>
[1] =>
[2] =>
[3] => 1231
23\'
[4] =>
[5] =>
)
)
If you are a php lover, you can copy the code of Regex to Capture Text Between Quotes Ver 2 in php, otherwise, you copy the main regex and use it in your own code.
Good luck.
Related Posts:
Regex to Capture Text Between Quotes
http://septiadi.com/2011/03/08/regex-to-capture-text-between-quotes-ver-2/