You can't use a backreference inside a character class because a character class matches exactly one character, and a backreference can potentially match any number of characters, or none.
What you're trying to do requires a negative lookahead, not a negated character class:
preg_match_all('/__\(([\'"])(?:(?!\1).)+\1\)/',
"__('match this') . 'not this'", $matches);
I also changed your alternation - \'|"
- to a character class - [\'"]
- because it's much more efficient, and I escaped the outer parentheses to make them match literal parentheses.
EDIT: I guess I need to expand that "more efficient" remark. I took the example Friedl used to demonstrate this point and tested it in RegexBuddy.
Applied to target text abababdedfg
,
^[a-g]+$
reports success after three steps, while
^(?:a|b|c|d|e|f|g)+$
takes 55 steps.
And that's for a successful match. When I try it on abababdedfz
,
^[a-g]+$
reports failure after 21 steps;
^(?:a|b|c|d|e|f|g)+$
takes 99 steps.
In this particular case the impact on performance is so trivial it's not even worth mentioning. I'm just saying whenever you find yourself choosing between a character class and an alternation that both match the same things, you should almost always go with the character class. Just a rule of thumb.