【问题标题】:Capture words on the right side of | (OR) in regex expression that are not in the left捕获右侧的单词| (OR) 正则表达式中不在左侧的
【发布时间】:2016-11-10 17:32:45
【问题描述】:

我正在尝试在此正则表达式的右侧捕获左侧未捕获的单词。

在下面的代码中,左侧在此字符串中捕获“17 英寸”:“this 235/45R17 is a 17 inch tyre”

(?<=([-.0-9]+(\s)(inches|inch)))|???????

但是,我放在右侧的任何内容,例如简单的 +w 都会干扰左侧

我如何告诉 RegEx 捕获任何单词,除非它是一个数字后跟英寸 - 在这种情况下捕获 17 和英寸?

【问题讨论】:

  • 和 Elasticsearch 有什么关系?你想用 Elasticsearch 和那个正则表达式做什么?
  • 谢谢,我正在构建一个标记器,基本上将字符串拆分为特定部分。可以在一个简单的空格上,也可以是数字和空格的组合(5 英寸)

标签: regex


【解决方案1】:

说明

((?:(?![0-9.-]+\s*inch(?:es)?).)+)|([0-9.-]+\s*inch(?:es)?)

** 要更好地查看图像,只需右键单击图像并选择在新窗口中查看

示例

现场演示

https://regex101.com/r/fY9jU5/2

示例文本

this 235/45R17 is a 17 inch tyre

示例匹配

  • 捕获组 1 将是与 17 inch 不匹配的值
  • Capture Group 2 将是英寸数
MATCH 1
1.  [0-20]  `this 235/45R17 is a `

MATCH 2
2.  [20-27] `17 inch`

MATCH 3
1.  [27-32] ` tyre`

说明

NODE                     EXPLANATION
----------------------------------------------------------------------
  (                        group and capture to \1:
----------------------------------------------------------------------
    (?:                      group, but do not capture (1 or more
                             times (matching the most amount
                             possible)):
----------------------------------------------------------------------
      (?!                      look ahead to see if there is not:
----------------------------------------------------------------------
        [0-9.-]+                 any character of: '0' to '9', '.',
                                 '-' (1 or more times (matching the
                                 most amount possible))
----------------------------------------------------------------------
        \s*                      whitespace (\n, \r, \t, \f, and " ")
                                 (0 or more times (matching the most
                                 amount possible))
----------------------------------------------------------------------
        inch                     'inch'
----------------------------------------------------------------------
        (?:                      group, but do not capture (optional
                                 (matching the most amount
                                 possible)):
----------------------------------------------------------------------
          es                       'es'
----------------------------------------------------------------------
        )?                       end of grouping
----------------------------------------------------------------------
      )                        end of look-ahead
----------------------------------------------------------------------
      .                        any character except \n
----------------------------------------------------------------------
    )+                       end of grouping
----------------------------------------------------------------------
  )                        end of \1
----------------------------------------------------------------------
 |                        OR
----------------------------------------------------------------------
  (                        group and capture to \2:
----------------------------------------------------------------------
    [0-9.-]+                 any character of: '0' to '9', '.', '-'
                             (1 or more times (matching the most
                             amount possible))
----------------------------------------------------------------------
    \s*                      whitespace (\n, \r, \t, \f, and " ") (0
                             or more times (matching the most amount
                             possible))
----------------------------------------------------------------------
    inch                     'inch'
----------------------------------------------------------------------
    (?:                      group, but do not capture (optional
                             (matching the most amount possible)):
----------------------------------------------------------------------
      es                       'es'
----------------------------------------------------------------------
    )?                       end of grouping
----------------------------------------------------------------------
  )                        end of \2
----------------------------------------------------------------------

【讨论】:

  • 非常感谢 - 这绝对让我走上了正确的道路。我得到的最终字符串是: (?
猜你喜欢
  • 2023-04-10
  • 2022-01-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-10
  • 1970-01-01
  • 2020-08-08
  • 1970-01-01
相关资源
最近更新 更多