Ning
Ning

Reputation: 595

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.

Upvotes: 0

Views: 49

Answers (2)

Liju
Liju

Reputation: 2303

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

Upvotes: 1

tripleee
tripleee

Reputation: 189317

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.

Upvotes: 0

Related Questions