微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

php – 可变长度的正则表达式

我的正则表达式如下:

(?<![\s]*?(\"|&quot;)")WORD(?![\s]*?(\"|&quot;))

正如你所看到的,我试图匹配WORD的所有实例,除非它们在“引号”内.所以…

WORD <- Find this
"WORD" <- Don't find this
"   WORD   " <- Also don't find this,even though not touching against marks
&quot;WORD&quot;  <- Dont find this (I check &quot; and " so works after htmlspecialchars)

我相信我的正则表达式会完美地工作如果我没有收到错误

Compilation Failed: lookbehind assertion is not fixed length

考虑到外观的局限性,有没有办法做我想做的事情?

如果您能想到其他任何方式让我知道.

非常感谢,

马修

附: WORD部分实际上包含Jon Grubers URL检测器

解决方法

我建议采用不同的方法.只要引号被正确平衡,这将起作用,因为你知道你在引用的字符串中iff后面的引号数是奇数,从而使lookbehind部分不必要:

if (preg_match(
'/WORD             # Match WORD
(?!                # unless it\'s possible to match the following here:
 (?:               # a string of characters
  (?!&quot;)       # that contains neither &quot;
  [^"]             # nor "
 )*                # (any length),("|&quot;)        # followed by either " or &quot; (remember which in \1)
 (?:               # Then match
  (?:(?!\1).)*\1   # any string except our quote char(s),followed by that quote char(s)
  (?:(?!\1).)*\1   # twice,)*                # repeated any number of times --> even number
 (?:(?!\1).)*      # followed only by strings that don\'t contain our quote char(s)
 $                # until the end of the string
)                  # End of lookahead/sx',$subject))

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐