【问题标题】:ruby regex to find nested matchesruby 正则表达式查找嵌套匹配
【发布时间】:2012-08-29 13:26:49
【问题描述】:

我的正则表达式是

/a .*programming .*test .*this/i

测试字符串是

This is a test This is a programming test This is a programming test in any language

我得到的匹配是

a test This is a programming test This

但是还有另一个较短的匹配“编程测试这个”,这个正则表达式无法找到,我使用扫描方法找到所有匹配但甚至无法捕获这个。 p>

非常感谢任何帮助

【问题讨论】:

    标签: ruby regex-greedy non-greedy


    【解决方案1】:

    RegexP 是一个非常复杂的东西!您确实需要知道字符串的哪些部分对匹配至关重要,哪些部分是关键。有很多方法可以完成一场比赛,但要猜出你真正想要的是什么并不容易:

    s = "This is a test This is a programming test This is a programming test in any language"
    s.match(/a [^a]*programming .*test .*this/i)[0]
     => "a programming test This" 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-10-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多