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.
You may use this regex to match characters between +
while ignoring \+
:
\+([^+\\]*(?:\\\+[^+\\]*)*)\+
RegEx Details:
\+
: Match opening +
(
: Start capture group #1
[^+\\]*
: Match 0 or more of any characters that are not +
or \
(?:
: Start non-capture group
\\\+
: Match a \+
[^+\\]*
: Match 0 or more of any characters that are not +
or \
)*
: End non-capture group. Repeat this group 0 or more times)
: End capture group #1\+
: Match closing +
Solution 2: A bit shorter but a bit slower regex would be:
\+((?:\\\+|[^+\\]+)*)\+
Details:
\+
: Match opening +
(
: Start capture group #1
(?:
: Start non-capture group
\\\+
: Match \
followed by a +
|
: OR[^+\\]+
: Match 1 or more of any characters that are not +
or \
)*
: End non-capture group. Repeat this group 0 or more times.)
: End capture group #1\+
: Match closing +