Blog > detail

Just my tiny mind

Regex to Capture Text Between Quotes

26-2-2012 Admin General 2 Comments

Regex to Capture Text Between Quotes

First of all, I am not an expert of regex, it should be better if you call me newbie.


In this post, I want to share a code of Regex to Capture Text Between Quotes. The code is written in php. However, you can copy and use the main regex code.


The main regex should be : 


/(?<!\\\)"(.*?)(?<!\\\)"|(?<!\\\)\'(.*?)(?<!\\\)\'/i


Then the PHP code is :


preg_match_all('/(?<!\\\)"(.*?)(?<!\\\)"|(?<!\\\)\'(.*?)(?<!\\\)\'/i', 'asdadd"123123\""asd@asda\"\""\"\"\""d@sdf "asda\"sd"'."asdadd'123123\''asd@asda\'\''\'\'\''d@sdf 'asda\'sd'", $matches);print_r($matches);


The code will capture all text between either single quote (') or double quotes ("), and will also capture \" or \' as the element of the text.


The code will give result like :


Array

(

[0] => Array

(

[0] => "123123\""

[1] => "\"\"\""

[2] => "asda\"sd"

[3] => '123123\''

[4] => '\'\'\''

[5] => 'asda\'sd'

)


[1] => Array

(

[0] => 123123\"

[1] => \"\"\"

[2] => asda\"sd

[3] =>

[4] =>

[5] =>

)


[2] => Array

(

[0] =>

[1] =>

[2] =>

[3] => 123123\'

[4] => \'\'\'

[5] => asda\'sd

)


)


If you are a PHP lover, you can copy the code of Regex to Capture Text Between Quotes 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 Ver 2

Regex to Capture Text Between Quotes Regex to Capture Text Between Quotes Regex to Capture Text Between Quotes

Regex to Capture Text Between Quotes Regex to Capture Text Between Quotes Regex to Capture Text Between Quotes

Regex to Capture Text Between Quotes

word word

Regex to Capture Text Between Quotes

Regex to Capture Text Between Quotes

Regex to Capture Text Between Quotes