加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

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

发布时间:2020-12-13 17:17:07 所属栏目: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
我的正则表达式如下:

(?<![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).)*1   # any string except our quote char(s),followed by that quote char(s)
  (??2(?!1).)*1   # twice,)*                # repeated any number of times --> even number
 (??3(?!1).)*      # followed only by strings that don't contain our quote char(s)
 $                # until the end of the string
)                  # End of lookahead/sx',$subject))

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读