【问题标题】:How can I match a numerical sequence of any length via regex?如何通过正则表达式匹配任意长度的数字序列?
【发布时间】:2020-04-16 17:51:07
【问题描述】:

我需要一个正则表达式来匹配任意长度的数字,后跟 .html

例如,示例匹配的字符串是:

4.html 32.html 46352.html

所以基本上任何数字后跟.html

【问题讨论】:

  • 请告诉我们你到目前为止做了什么,你到底在哪里遇到了困难。
  • 我正在使用带有冷融合的正则表达式。我可以匹配数字序列,但无法匹配.html。这就是我所拥有的并且正确匹配数字:REFind("\d+", "My page is 123.html!hey 7689") 我一直在尝试这个:REFind("\d+/^.html$/", “ColdFusion 123.html!嘿 7689”)但不匹配 123.html
  • @paddyc - 你能把代码 sn-p 移到问题中,让它更明显,然后删除评论吗?

标签: regex coldfusion


【解决方案1】:

console.log("ColdFusion 123.html! hey 7689".match(/\d+\.html/)[0])

或者在你的情况下:REFind("\d+\.html", "ColdFusion 123.html! hey 7689")

“\d+/^.html$/”中的^ 不会转义.。要转义.,您需要使用\$ 也表示“字符串的结尾”,123.html 不是结尾。而且我不知道^.html$ 周围的/ColdFusion 中是什么意思,但它们对我来说确实很奇怪。

【讨论】:

    【解决方案2】:

    如果 regex 是可选的,您可以使用 ColdFusion 函数执行此操作:

    if (
    listlen(yourstring, '.') == 2 
    &&
    listlast(yourstring, '.') == 'html'
    &&
    isnumeric(listfirst(yourstring, '.')) == true
    )
    code for match
    else
    code for no match
    

    【讨论】:

      【解决方案3】:
       REFindNoCase("\d+\.html", str, 1, 1);
      

      这将为您提供至少一个数字,后跟文字字符串“。”后跟文字字符串“html”。

      如果您希望它区分大小写,则只需使用 REFind() 版本:

       REFind("\d+\.html", str, 1, 1);
      

      就是这样!

      【讨论】:

        【解决方案4】:

        如果您在文本中搜索多个匹配项,您应该/可以使用 rematch 或 rematchnocase(不区分大小写),如下所示:

        <Cfdump var="#rematch("\d+\.html", "Test string 123.html! testing a little mor bla.html. Stil testing 1.html")#">
        

        您可以在此处查看在线示例: https://trycf.com/gist/5426f17eda973b3819efefc04e8fd311/lucee5?theme=monokai

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2011-12-22
          • 2011-06-17
          • 2018-10-22
          • 2020-12-01
          • 1970-01-01
          • 1970-01-01
          • 2011-11-16
          相关资源
          最近更新 更多