I have content like foo == 'bar test baz' and test.asd = "buz foo". I need to match the “identifiers”, the ones on the left that are not within double/single quotes. This is what I have now:
preg_replace_callback('#([a-zA-Z\\.]+)#', function($matches) {
var_dump($matches);
}, $subject);
It now matches even those within strings. How would I write one that does not match the string ones?
Another example: foo == 5 AND bar != 'buz' OR fuz == 'foo bar fuz luz'. So in essence, match a-zA-Z that are not inside strings.
would work on your examples. It matches any number of characters (starting at the start of the string) that are neither quotes nor equals signs.
additionally avoids matching whitespace which may or may not be what you need.
Edit:
You’re asking how to match letters (and possibly dots?) outside of quoted sections anywhere in the text. This is more complicated. A regex that can correctly identify whether it’s currently outside of a quoted string (by making sure that the number of quotes, excluding escaped quotes and nested quotes, is even) looks like this as a PHP regex:
An explanation can be found here. But probably a regex isn’t the right tool for this.