我试图捕获这个正则表达式右侧没有捕获到左侧的单词。捕捉|右侧的词(或)在正则表达式不是在左边
在下面的代码,左侧捕获“17英寸”在此字符串:“这235/45R17的17寸轮胎”
(?<=([-.0-9]+(\s)(inches|inch)))|???????
然而,什么我把在右侧,这样的作为一个简单的+ w是干扰左侧
我如何告诉RegEx捕获任何单词,除非它是一个数字后面英寸 - 在这种情况下捕获17和英寸?
我试图捕获这个正则表达式右侧没有捕获到左侧的单词。捕捉|右侧的词(或)在正则表达式不是在左边
在下面的代码,左侧捕获“17英寸”在此字符串:“这235/45R17的17寸轮胎”
(?<=([-.0-9]+(\s)(inches|inch)))|???????
然而,什么我把在右侧,这样的作为一个简单的+ w是干扰左侧
我如何告诉RegEx捕获任何单词,除非它是一个数字后面英寸 - 在这种情况下捕获17和英寸?
((?:(?![0-9.-]+\s*inch(?:es)?).)+)|([0-9.-]+\s*inch(?:es)?)
**要看到图像更好,只需右键点击新窗口
现场演示的图像,然后选择视图
https://regex101.com/r/fY9jU5/2
示例文本
this 235/45R17 is a 17 inch tyre
样品匹配
17 inch
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
----------------------------------------------------------------------
非常感谢 - 这绝对能让我走上正确的道路。我得到的最终字符串是:(?<=([ - 。0-9] +(\ s)(inch)))|(?<!([ - 。0-9]))\ s +(?!= (英寸))如果前缀和后缀缺失,则右侧匹配 – hitwill
如果您满意,请将答案标记为已接受。 –
它更容易,更安全的没什么只是先更换所有不想要的东西。
只有匹配你正在寻找的东西。
例如在此JavaScript例如:
var str = "this 235/45R17 is a 17 inch tyre of more than 9 inches.";
var result = str.replace(/\s[\d.\-]+\sinch(?:es)?/gi, "").match(/\-?\d+\.?\d*/gi);
为了得到结果235,45,17
负先行是可能的,但最好使用词语边界\湾
为了避免像仍然匹配数字中不应该匹配的第一个数字的问题。
例如:
var result = str.match(/(?:\-?\d+\.?\d*)(?:[a-z]|\b)(?!\s+inch(?:es)?)/gi);
为了让结果235,45R,17
,什么是与Elasticsearch连接?你想用Elasticsearch和那个正则表达式来做什么? –
谢谢,我正在构建一个标记器来基本上在特定的部分中分割一个字符串。无论是在简单的空间上,还是数字和空间的组合(5英寸) – hitwill