0

I have an error in my regex, that somebody wrote for me. I've tried to write my own regex but this is just to hard for me to crack.

I have a custom "tag" like {module:agenda:getlist:params(2)} But.. if I only have 1 param the regex doesn't see the 2 but gets the wrong code.

This is my regex code

$paramcount = preg_match_all('{
        # [^,]+ everything that isn't a comma
        # (?<=...) is a look behind. Meaning that the part that has the 3 dots 
        # becomes matched but not goes through the rest of the regex

        # matches "null" in "params(null"
        (?<=params\()[^,]+

        | # this is the separation dash

        # (?=...) is a look ahead, same as the look behind but than on the end

        # matches "null" in " null)"
        (?<= )[^,]+(?=\))

        |

        # matches "true" in ", true" and ""foo"" in ", "foo""
        (?<=, )[^,]+
    }x', $data, $parammatches);

So when I try to match it.. the outcome is:

$array = array
    (
        [0] => params(2
    );

This is not what i want, i only want the 2 to be matched not the rest. When i give it more params like this "params(null, 2)" everything works out and i have 2 nice array values.

Can somebody guide me or help me out here.

EDIT: More output can be found on pastebin. http://pastebin.com/x0WL9K4u

4
  • What is it exactly that you want ? Extract all parameters passed to the param between parentheses and put them in the array ? Commented Aug 10, 2013 at 21:39
  • Yes that i what i want. Actually i want to get everything between the hooks "params(i-want-these-values)"
    – Rickerd
    Commented Aug 10, 2013 at 21:39
  • Can you please post more sample input ? Commented Aug 10, 2013 at 21:59
  • More output can be found on pastebin. pastebin.com/x0WL9K4u
    – Rickerd
    Commented Aug 10, 2013 at 22:26

1 Answer 1

0

Somebody answerd my question. He wrote an update for my regex problem

<?php
$str = 'params(null, true, "Foo", null)';

preg_match_all('{
# [^,]+ betekend alles wat geen komma is
# (?<=...) is een look behind. Dit betekend dat het gedeelte op de 3 puntjes
# gematched moet worden voor de rest van de regex, maar dat hij niet meegenomen
# wordt in de match

# matches "null" in "params(null"
(?<=params\()[^,]+

| # dit is het scheidingsteken

# (?=...) is een look ahead, zelfde als look behind maar dan voor erachter

# matches "null" in " null)"
(?<= )[^,]+(?=\))

|

# matches "true" in ", true" and ""foo"" in ", "foo""
(?<=, )[^,]+
}x', $str, $matches);

$params = $matches[0];
?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.