phpregexrecursive-regex

How to find many instances of a specific pattern in RegEx?


Currently (in PHP) I have the following regex pattern:

\[(.*) (.*)=(.*)\]

This matches [doSomething limitation=true]

The end result being that my code will interpret that string and replace it with whatever value is coded to return for it.

However, some of my code needs multiple variables sent through to the function, for example:

[doSomething limitation=true otherlimitation=false sendfile=1 title="hello there"]

How can I make the (.)=(.) in the regex repeatable so that it matches every variable sent through including the first (most important) name of function?


Solution

  • The following may work for you:

    \[(.*) ((.*)=(.*))+\]
    

    You may also want to replace your asterisks with plus-signs. Currently, your regex would match [ =] as a valid string.

    \[(.+) ((.+)=(.+))+\]