说明
([0-9]+)(?:-([0-9]+)|\s*over)
** 要更好地查看图像,只需右键单击图像并选择在新窗口中查看
此正则表达式将执行以下操作:
示例
现场演示
https://regex101.com/r/hE5dL4/2
示例文本
注意:关于I-75的边缘情况
'm trying to match speed descriptions of highway tickets, for example,text lines:
"L A 16-25MPH" should return 2 groups: 16, 25
"LIMITED ACCESS SPEED I-75" should return no matches.
"LMT ACC 6-10" should return 2 groups: 6, 10
"6 OVER" should return 1 group: 6
I'm OK with all of the above situations, but I run into issues for strings with numbers that aren't related to speed, for example:
"LIMITED ACCESS SPEED I-75" should return no matches.
示例匹配
MATCH 1
1. [89-90] `16`
2. [91-93] `25`
MATCH 2
1. [193-194] `6`
2. [195-197] `10`
MATCH 3
1. [231-232] `6`
说明
NODE EXPLANATION
----------------------------------------------------------------------
( group and capture to \1:
----------------------------------------------------------------------
[0-9]+ any character of: '0' to '9' (1 or more
times (matching the most amount
possible))
----------------------------------------------------------------------
) end of \1
----------------------------------------------------------------------
(?: group, but do not capture:
----------------------------------------------------------------------
- '-'
----------------------------------------------------------------------
( group and capture to \2:
----------------------------------------------------------------------
[0-9]+ any character of: '0' to '9' (1 or
more times (matching the most amount
possible))
----------------------------------------------------------------------
) end of \2
----------------------------------------------------------------------
| OR
----------------------------------------------------------------------
\s* whitespace (\n, \r, \t, \f, and " ") (0
or more times (matching the most amount
possible))
----------------------------------------------------------------------
over 'over'
----------------------------------------------------------------------
) end of grouping
----------------------------------------------------------------------