Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple regex capture groups using quantifiers

Tags:

regex

php

Is there a way to get multiple capture groups out of a regex that is using quantifiers? For example, say I have this data (simplified from what I have to deal with):

<td>Data 1</td>
<td>data 2</td>
<td>data 3</td>
<td>data 4</td>

Right now, if I write a regex like this:

(?:<td>(.+?)<\/td>\s*){4}

I end up with only one capture group, the last one "data 4". Is there a way to use the quantifier and end up with 4 capture groups, or am I forced to write the regex like this to get what I want:

<td>(.+?)<\/td>\s*<td>(.+?)<\/td>\s*<td>(.+?)<\/td>\s*<td>(.+?)<\/td>

Yes, I am well aware that I can hack this simple example up much easier programmatically and then apply and necessary regexes or simpler pattern matches. The data I am working with is far more complex and I would really like to use a regex to handle all of the parsing.

like image 272
Tony Lukasavage Avatar asked May 16 '11 13:05

Tony Lukasavage


1 Answers

With php you can use preg_match_all :

$str = '<td>Data 1</td>
<td>data 2</td>
<td>data 3</td>
<td>data 4</td>
';
preg_match_all('/(?:<td>(.+?)<\/td>\s*)/', $str, $m);
print_r($m);

output:

Array
(
    [0] => Array
        (
            [0] => <td>Data 1</td>

            [1] => <td>data 2</td>

            [2] => <td>data 3</td>

            [3] => <td>data 4</td>

        )

    [1] => Array
        (
            [0] => Data 1
            [1] => data 2
            [2] => data 3
            [3] => data 4
        )

)
like image 199
Toto Avatar answered Oct 05 '22 19:10

Toto