Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
@name "@new other content" "@" @here and here@
我想匹配所有的“ @ ”,不包括引号之间的那些。
我试过:
(?!\")\@(?!\")
它部分工作,不匹配“@”但匹配“@new 其他内容”。
我在 PHP 中使用“preg_replace”。
谢谢你的帮助。
更新:
在这种情况下,我只想匹配 @ on: @name, @here, here@...
@name, @here, here@
如果“@”总是直接在“之后,那么您可以使用
(?<!\")\@
在这一点上你(?!\")\@(?!\")错了,它必须是一个否定的后向断言((?<!...))。如果“@”后面直接跟一个“.
(?<!...)
如果“@”可以在引号之间的任何位置,那么 php 中的正则表达式是不可能的。
假设报价总是平衡的,你可以试试这个
[^"\s]*@[^"\s]*(?=[^"]*(?:"[^"]*"[^"]*)*$)
在 Regexr 上查看
当前面有偶数个 " 时,这只是匹配包含 @ 的子字符串。
请注意:当字符串中的任何位置存在不匹配的引号时,它将失败。