我正在尝试捕获此正则表达式右侧未在左侧捕获的单词。
在下面的代码中,左侧捕获" 17英寸"在这个字符串中:"这个235 / 45R17是一个17英寸的轮胎"
(?<=([-.0-9]+(\s)(inches|inch)))|???????
然而,我放在右侧的任何东西,例如简单的+ w都会干扰左侧
我如何告诉RegEx捕获任何单词,除非它是一个数字后跟英寸 - 在这种情况下捕获17和英寸?
答案 0 :(得分: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
样本匹配
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
----------------------------------------------------------------------
答案 1 :(得分:0)
首先更换所有不需要的东西更简单,更安全 然后才匹配你要找的东西。
例如在这个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
可能出现负面预测,但最好使用边界\ b。
避免仍然匹配不匹配的数字中第一个数字的问题。
例如:
var result = str.match(/(?:\-?\d+\.?\d*)(?:[a-z]|\b)(?!\s+inch(?:es)?)/gi);
提供结果235,45R,17