Search code examples
phpregexpreg-matchliteralstextmatching

Match parenthetical expressions in a string


I am trying to make a php file so it can accept a word for a regex (a+b)(ab+ba)* I am using preg_match and so far I've come up with this:

$subject = "a+b";
$pattern = '(([a]{1}\+[b]{1})?([ab]{1}\+[ba]{1}))';
preg_match($pattern, $subject, $matches);
print_r($matches);

I am not sure if I completely understand how it works but it's been few hours now and I am still trying to figure it out. How do I make it so it fullfills my condition?

I want to match (a+b)(ab+ba)* where the first bracket (a+b) is required and the * on the second bracket (ab+ba) means that there could be zero or multiple instances of it. It should work like this:

$subject= "(a+b)"
Match

$subject= "(a+b)(ab+ba)"
Match


$subject= "(a+b)(ab+ba)(ab+ba)"
Match

$subject= "(ab+ba)"
No Match

$subject= ""
No Match

Solution

  • In (([a]{1}\+[b]{1})?([ab]{1}\+[ba]{1}))

    [a]{1} (1 character in a character class) can be written as a

    [b]{1} can be written as b

    [ab] (2 characters in a character class) means a or b

    I think you have to escape the opening parenthesis \(, or else you would start a capturing group.

    If I am not mistaken, this might match what you are looking for:

    ^\(a\+b\)(?:\(ab\+ba\))*$

    The second part is in a non capturing group (?:\(ab\+ba\))* and repeated zero or more times.

    Php test ouput