shyam shyam - 5 months ago 20x
PHP Question

regex back-reference not working in PHP PCRE

I want to match matching tags like

. I tried the regex


but this fails. The expression worked when I used the exact text inside the angle brackets, i.e,


works, but even



I'm assuming that the back reference is not working here.

Can someone help me out please. Thanks

PS: I'm not using this to parse HTML. I know I shouldn't.


You didn't show your PHP code, but I surmise you have your regex in double quotes. If so then the backreference \1 actually is converted into an ASCII character before it reaches PCRE. (All \123 sequences are interpreted as C-string octal escapes there.)