0

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.

1
  • Try re.compile(r'(?<![0-9]{2})[0-9]{1,2}-[0-9]') Commented Sep 6, 2020 at 10:11

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.