Regex to Capture Text Between Quotes Ver 2

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&quot;1231<br />
23\&quot;&quot;asd@asda\&quot;\&quot;&quot;\&quot;\&quot;\&quot;&quot;d@sdf  &quot;asda\&quot;sd&quot;'.&quot;asdadd'1231<br />
23\''asd@asda\'\''\'\'\''d@sdf  'asda\'sd'&quot;;<br />
preg_match_all('/(?&lt;!\\\)&quot;(.*?)(?&lt;!\\\)&quot;|(?&lt;!\\\)&quot;(.*\n.*?)(?&lt;!\\\)&quot;|(?&lt;!\\\)\'(.*?)(?&lt;!\\\)\'|(?&lt;!\\\)\'(.*\n.*?)(?&lt;!\\\)\'/i',   $str, $matches);print_r($matches);<br />

The code will give result like:

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

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

    [2] =&gt; Array
        (
            [0] =&gt; 1231
23\"
            [1] =&gt;
            [2] =&gt;
            [3] =&gt;
            [4] =&gt;
            [5] =&gt;
        )

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

    [4] =&gt; Array
        (
            [0] =&gt;
            [1] =&gt;
            [2] =&gt;
            [3] =&gt; 1231
23\'
            [4] =&gt;
            [5] =&gt;
        )

)

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/

Advertisements

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: