phpregexpreg-match-all

Regex find anything between + sign except \+


With regex, I want to get anything between + sign avoid if + with backslash.

My string is GroupNo+ \+ +Name+ / +Ac

I want to get " \\+ " and " / " with php preg_matchall

I tried with /\+(.*?)\+/ but it takes + with slash also.


Solution

  • You may use this regex to match characters between + while ignoring \+:

    \+([^+\\]*(?:\\\+[^+\\]*)*)\+
    

    RegEx Demo

    RegEx Details:


    Solution 2: A bit shorter but a bit slower regex would be:

    \+((?:\\\+|[^+\\]+)*)\+
    

    RegEx Demo 2

    Details: