Regex to Capture Text Between Quotes

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] =&gt; Array
        (
            [0] =&gt; "123123\""
            [1] =&gt; "\"\"\""
            [2] =&gt; "asda\"sd"
            [3] =&gt; '123123\''
            [4] =&gt; '\'\'\''
            [5] =&gt; 'asda\'sd'
        )

    [1] =&gt; Array
        (
            [0] =&gt; 123123\"
            [1] =&gt; \"\"\"
            [2] =&gt; asda\"sd
            [3] =&gt;
            [4] =&gt;
            [5] =&gt;
        )

    [2] =&gt; Array
        (
            [0] =&gt;
            [1] =&gt;
            [2] =&gt;
            [3] =&gt; 123123\'
            [4] =&gt; \'\'\'
            [5] =&gt; 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/

 

 

Advertisements

One response to “Regex to Capture Text Between Quotes

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: