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

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了php – 可变长度的正则表达式脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
我的正则表达式如下:

(?<![\s]*?(\"|&amp;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))

脚本宝典总结

以上是脚本宝典为你收集整理的php – 可变长度的正则表达式全部内容,希望文章能够帮你解决php – 可变长度的正则表达式所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。