简体   繁体   中英

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. re.compile(r'[0-9]+-[0-9]') works but how can I modify to eliminate 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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