Blog > detail

Just my tiny mind

Regex to Capture Text Between Quotes Ver 2

26-2-2012 Admin General 2 Comments

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 /(?

Then the PHP code is:



$str = 'asdadd"1231

23\""asd@asda\"\""\"\"\""d@sdf "asda\"sd"'."asdadd'1231

23\''asd@asda\'\''\'\'\''d@sdf 'asda\'sd'";

preg_match_all('/(?<!\\\)"(.*?)(?<!\\\)"|(?<!\\\)"(.*\n.*?)(?<!\\\)"|(?<!\\\)\'(.*?)(?<!\\\)\'|(?<!\\\)\'(.*\n.*?)(?<!\\\)\'/i', $str, $matches);print_r($matches);


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

Regex to Capture Text Between Quotes Ver 2 Regex to Capture Text Between Quotes Ver 2 Regex to Capture Text Between Quotes Ver 2

Regex to Capture Text Between Quotes Ver 2 Regex to Capture Text Between Quotes Ver 2 Regex to Capture Text Between Quotes Ver 2

Regex to Capture Text Between Quotes Ver 2

word word

Regex to Capture Text Between Quotes Ver 2

Regex to Capture Text Between Quotes Ver 2

Regex to Capture Text Between Quotes Ver 2