我试图匹配高速公路车票的速度描述,例如,文本行:
“L A 16-25 mph”应返回2组:16、25
“LMT ACC 6-10”应返回2组:6、10
“6过”应返回1组:6
我对上面所有的情况都没问题,但是我遇到了与速度无关的数字字符串的问题,例如:
“LIMITED ACCESS SPEED I-75”不应返回匹配项。
我能获得的最接近的表达式是:((?<!\w-)\d+)[^\d]*((?<!\w-)\d+)?
,它将匹配1组:5,使用python正则表达式引擎
现在,可以肯定地假设一个字母然后一个连字符(\w-
)是我试图使用负回溯来排除的,我只是不知道如何组合多个数字(\d+
)来使用负回溯。你知道吗
Negative lookbehinds必须具有固定的长度(有关详细信息,请参见Python doc),因此
\d+
之类的内容是不允许的。你知道吗不过,您可以使用lookback检查高速公路的某些固定宽度指示器,例如
I
和RT
(不区分大小写)。您可能还需要添加额外的案例,以便在破折号周围添加空格,完全没有破折号,等等一个可能符合条件的选项(设置不敏感标志):
(?<!i|rt)(\d+)
说明
**要更好地查看图像,只需右键单击图像并选择“在新窗口中查看”
此正则表达式将执行以下操作:
示例
现场演示
https://regex101.com/r/hE5dL4/2
示例文本
注:关于
I-75
的边大小写样本匹配
解释
相关问题 更多 >
编程相关推荐