【问题标题】:How to create a regex that will match anything that has 2 dimensions but will not match anything over that?如何创建一个匹配任何二维但不匹配任何二维的正则表达式?
【发布时间】:2021-10-17 23:00:12
【问题描述】:

我正在使用一个 Java 程序,我正在尝试创建一个正则表达式,该表达式将匹配任何具有二维的内容,并且不会匹配任何超过该维度的内容。

我还试图指示两个捕获组,以便我可以分别捕获每个维度值。尺寸必须按以下格式输入Some number x Other number(由x分隔)

这里有一些例子可以更好地解释我想要做什么:

  • S (26 in x 30 in):这应该匹配并捕获 26 和 30。
  • 20 x 40:这应该匹配并捕获 20 和 40。
  • Standard Size (10 x 40):这应该匹配并捕获 10 和 40。
  • 20 x 40 x 80:这根本不应该匹配。
  • M (26 in x 30 in x 50 in):这根本不应该匹配。

我已经尝试过这个正则表达式,但它仍然认为 3 个维度是有效的:

([\d.,]+).*?[xX] ?([\d,.]+).*

【问题讨论】:

    标签: java regex


    【解决方案1】:

    我认为这应该可以满足您的需求:

    ^[^\d]*(\d+)[^\dx]*[xX][^\dx]*(\d+)[^\d]*$
    

    可能会出现一种极端情况,即x 不是两个数字之间的分隔符。不确定这是否是您的用例的问题。

    你可以在这里看到它在regex101

    【讨论】:

      【解决方案2】:

      试试这个:

      ^(?!.* x .* x ).*?(\d+)(?: [a-z]+)? x (\d+)
      

      live demo

      由于锚定到开始 ^(?!.* x .* x ) 的负前瞻,阻止了超过 2 个维度的匹配

      【讨论】:

        【解决方案3】:

        请注意,我已经在下面的评论中解决了 OP 要求的原始问题的变体。

        我还假设在以下示例字符串中进行匹配:

        Extra x here is OK 31.6 x more chars a-w, y-z here is OK 81.7 same here
                           ^^^^                                  ^^^^
        
        Numbers 78 and 81.1 before 22.5 x 31 is OK
                                   ^^^^   ^^
        

        你可以使用表达式

        ^(?!.*(?:\d+(?:\.\d+)?[^\dx]*x[^\dx]*){2}\d).*(?<![\d.])(\d+(?:\.\d+)?)[^\dx]*x[^\dx]*(?<![\d.])(\d+(?:\.\d+)?)
        

        Demo

        以下操作由正则表达式引擎执行。

        ^                 # match beginning of the string
        (?!               # begin negative lookahead
          .*              # match 0+ chars
          (?:             # begin non-capture group
            \d+(?:\.\d+)? # match 1+ digits optionally followed by a period and 1+ digits
            [^\dx]*       # match 0+ chars other than digits and 'x'
            x             # match 'x'
            [^\dx]*       # match 0+ chars other than digits and 'x'
          ){2}            # end non-capture group and execute twice
          \d              # match a digit
        )                 # end negative lookahead
        .*                # match 0+ digits
        (?<!              # begin negative lookbehind
          [\d.]           # match a digit or period
        )                 # end negative lookbehind
        (                 # begin capture group 1
          \d+(?:\.\d+)?   # match 1+ digits optionally followed by a period and 1+ digits
        )                 # end capture group 1
        [^\dx]*           # match 0+ chars other than digits and 'x'
        x                 # match 'x'
        [^\dx]*           # match 0+ chars other than digits and 'x'
        (?<!              # begin negative lookbehind
          [\d.]           # match a digit or period
        )                 # end negative lookbehind
        (                 # begin capture group 2
          \d+(?:\.\d+)?   # match 1+ digits opinion followed by a period and 1+ digits
        )                 # end capture group 2
        

        请注意,表达式以否定前瞻开头,断言没有表单的子字符串

        ddd ... x ... ddd ... x ... ddd
        

        通过首先满足这个要求,随后捕获两个感兴趣的子字符串变得更容易。

        【讨论】:

        • 谢谢。这似乎工作得很好。唯一的事情是我无法添加对十进制值的支持。例如:“20.5 x 40.5”应该是有效的
        • 我做了你要求的改变。
        猜你喜欢
        • 1970-01-01
        • 2016-10-14
        • 2020-12-20
        • 2020-10-13
        • 2014-03-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多