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

正则表达式 – 不包括单词的ColdFusion正则表达式

发布时间:2020-12-14 05:47:21 所属栏目:百科 来源:网络整理
导读:我在使用ColdFusion 10构建正则表达式时遇到了麻烦.如果URL在其中包含“mydomain.com”的任何子域的末尾包含“dev”,则需要reFind()返回零. reFind(THE_REGEX,"subdomaindev.mydomain.com") needs to return 0 reFind(THE_REGEX,"subdomain.mydomain.com") n
我在使用ColdFusion 10构建正则表达式时遇到了麻烦.如果URL在其中包含“mydomain.com”的任何子域的末尾包含“dev”,则需要reFind()返回零.

reFind(THE_REGEX,"subdomaindev.mydomain.com") needs to return 0
    reFind(THE_REGEX,"subdomain.mydomain.com") needs to return a positive number

我在Adobe的文档中找到了以下内容:
(http://help.adobe.com/en_US/ColdFusion/10.0/Developing/WSc3ff6d0ea77859461172e0811cbec0a38f-7ffb.html)并基于此我尝试使用前瞻概念.

认为这会起作用,但它不会:

reFind("(?!dev).mydomain.com$","subdomaindev.mydomain.com") = 13
    reFind("(?!dev).mydomain.com$","subdomain.mydomain.com") = 10

不明白为什么这两个都给零:

reFind("(?=dev).mydomain.com$","subdomaindev.mydomain.com") = 0
    reFind("(?=dev).mydomain.com$","subdomain.mydomain.com") = 0

这是我期望的结果(?=):

reFind("(?:dev).mydomain.com$","subdomaindev.mydomain.com") = 10
    reFind("(?:dev).mydomain.com$","subdomain.mydomain.com") = 0

注意:这是用于ColdBox的环境配置,我只能将一个正则表达式传递给一个名为“环境”的变量,然后调用匹配环境的方法.我宁愿不再用该方法检查“dev.”,但如果我必须,我会.

感谢您的任何帮助!

解决方法

(评论太长)

Don’t understand why this gives zero for both

reFind("(?=dev).mydomain.com$","subdomaindev.mydomain.com") = 0

说实话,我也没有.但是,我遇到了this thread,这提供了一个似是而非的解释.解释(使用你的价值观):

Look-aheads look forward from the character at which they are placed —
and you’ve placed it before the .. So,what you’ve got is actually
saying “anything ending in .mydomain.com as long as the first three characters
starting at that position (.my) are not dev” which is always true.

..或者在(?= dev)的情况下,总是假的,因为显然字符.my永远不能等于dev.

进一步搜索出现了Adam Cameron关于regular expressions and look arounds的详细博客文章.“负面预测”部分包含用于确认字符串不包含单词CAT的表达式的示例:

^(?!.*CAT).*$

博客条目提供了更好的解释,但基本上它利用^(开始),$(结束)和.*(零个或多个字符) – 来搜索整个字符串.而您当前的表达式仅搜索紧随其后的字符,即“.mydomain.com”.

如果我理解正确,您可以使用上面的方法确认提供的字符串不以“dev.mydomain.com”结尾.只需将“CAT”更改为您要匹配的子字符串…错误…不匹配.没有经过高度测试,但这些方面的内容如下:

reFind("^(?!.*dev.mydomain.com$).*$","subdomain.mydomain.com")
reFind("^(?!.*dev.mydomain.com$).*$","subdomaindev.mydomain.com")

结果:

> 0 ==> “subdomaindev.mydomain.com”
> 1 ==> “subdomain.mydomain.com”

免责声明:在任何想象中,我都不是一个正则表达式专家,因此完全有可能有更好的选择.但是,希望这有助于解释为什么当前表达式不按预期方式工作.

更新:

正如评论中所述,@ zabuuq的最终工作表达是:

^(?!.*dev.mydomain.com).*.mydomain.com$

(编辑:李大同)

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

    推荐文章
      热点阅读