I was writing very complex regex and tested it using Rubular editor and after few hours I realize that it doesn’t work in PHP.
This is just tiny part of longer regex pattern:
((\\\\|\\\|)|[^\\\|])*?
I have to mach string under following conditions.
- String may contain
|character only escaped as\|. - String may contain
\character only escaped as\\. - String may be empty.
This means that strings like
test
test\\
test\|
t\\e\|s\|\|t\\\\\\\\\\
are CORRECT, and strings like
test\
test|\
\
|
test\\\\|
are NOT CORRECT
Also, Rubular accepts [^\\\|] and [^\|\\] (same). PHP doesn’t work with [^\|\\]. Why?
Is here something I don’t see because I think that both characters have been escaped correctly but PHP doesn’t agree with me.
I’m still new in this regex thing, so don’t blame my stupidity.
Thanks in advance!
You need to double escape the \ when quoting in PHP.