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
http://septiadi.com/2011/03/07/regex-to-capture-text-between-quotes/
March 8th, 2011 at 10:19 am
[...] 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 [...]