tags:

views:

29

answers:

3

P/S: I am a PHP programmer. Given:

div{3|5|6|9}[id = abc| class=image], a[id=link|class=out]

I want to use regex to generate a result as an array, e.g:

array(

  [div] => array(
                "3|5|6|9",
                "id = abc| class=image"
                )

  [a] => array(
                "",
                "id=link|class=out")

)

Would anyone please help? Thank you a lot!

A: 

If you can guarantee that a comma will not exist between { and } and between [ and ], you can first split the string by , and then use this regular expression:

/([a-z]+)(\{(.*?)\})?\[(.*?)\]/

The captured groups that you want are $1, $3, and $4 (those back-reference numbers should match up if you use preg_match_all)

Note: I tested this out in Javascript.

Vivin Paliath
+1  A: 

Have a try with this:

$str='div{3|5|6|9}[id = abc| class=image], a[id=link|class=out]';

preg_match_all('/(\w+)(\{(.*?)\})?\[(.*?)\](?:, |$)?/', $str, $m);

$out = array($m[1][0] => array($m[3][0], $m[4][0]), $m[1][1] => array($m[3][1], $m[4][1]));

print_r($out);

Output:

Array
(
    [div] => Array
        (
            [0] => 3|5|6|9
            [1] => id = abc| class=image
        )

    [a] => Array
        (
            [0] =>
            [1] => id=link|class=out
        )

)
bourbaki
A: 
preg_match_all('/(\w+)(\{(.*?)\})?\[(.*?)\](?:, |$)?/', $str, $m);

I believe the above is fine to work unless another string like:

$str='div{3|5|6|9}[id = abc| class=image], a[id=link|class=out], br, ul';

the regex does not capture the br and ul.

vnmarser