Reputation: 850
@name "@new other content" "@" @here and here@
I want to match all the "@" excluding the ones in between quotes.
I tried:
(?!\")\@(?!\")
It works partially, does not match "@" but does match "@new other content".
I am using with "preg_replace" in PHP.
Thanks for any help.
UPDATE:
In this case I want to match the @ on: @name, @here, here@
only...
Upvotes: 1
Views: 180
Reputation: 92986
If the "@" is always directly after the " then you can use
(?<!\")\@
Your (?!\")\@(?!\")
is wrong at that point, it has to be a negative lookbehind assertion ((?<!...)
). and the following lookahead assertion is only failing, if the "@" is directly followed by a ".
If the "@" can be anywhere between quotation marks, then it is not possible with regex in php.
Under the assumption, that the quotes are always balanced you can try this
[^"\s]*@[^"\s]*(?=[^"]*(?:"[^"]*"[^"]*)*$)
See it here on Regexr
This simply matches a substring that contains a @, when there is an even amount of " ahead.
Be careful: it will fail when there is an unmatched quotation mark anywhere in the string.
Upvotes: 3