简体   繁体   English

正则表达式 - 如何消除python中的某些模式

[英]Regular expression - How to eliminate certain pattern in python

I have some articles containing match scores like 13-9, 34-12, 22-10 which I want to extract using a regular expression to find the pattern in Python.我有一些文章包含诸如 13-9、34-12、22-10 之类的匹配分数,我想使用正则表达式提取这些分数以在 Python 中查找模式。 re.compile(r'[0-9]+-[0-9]') works but how can I modify to eliminate 1999-06, 2020-01? re.compile(r'[0-9]+-[0-9]')有效,但如何修改以消除 1999-06, 2020-01? I tried re.compile(r'[0-9]{1,2}-[0-9]') but those year values return as 99-06 which is also invalid in my case.我试过re.compile(r'[0-9]{1,2}-[0-9]')但那些年份值返回为 99-06 这在我的情况下也是无效的。

You can match for exact number of digits required with look behind assertions, not to slice log numbers, like below您可以匹配查看断言所需的确切位数,而不是对日志编号进行切片,如下所示

(?<!\d)\d{2}-\d{1,2}

Demo演示

You can avoid matching in the middle of a number with您可以避免在数字中间匹配

r'(?<!\d)[0-9]{1,2}-[0-9]'

The negative lookbehind prohibits matching immediately after another digit.负向后视禁止在另一个数字之后立即匹配。

Perhaps also add也许还添加

(?!\d)

at the end to impose a similar restriction at the end of the match.在比赛结束时施加类似的限制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM