Category Archives: regex

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/


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/