Reputation: 73
I am currently trying to extract dates from strings. Here are a few examples:
02.10 abcdef -> extract '02.10'
abcdef 03.12 -> extract '03.12'
abcdef 308.56 -> extract nothing
A simple regex such as (\d{2}.\d{2})
works fine for the first two cases but I catch a false positive for the third example, the regex returns 08.56
, which makes sense.
Is there any way to prevent this string from being extracted? I tried [^0-9](\d{2}.\d{2})
which seems to be working on regex debugging websites but not when I compile it as a python regex with
import re
regex = re.compile(r'[^0-9](\d{2}.\d{2})')
Thanks in advance
Upvotes: 0
Views: 152
Reputation: 320
I First thought you needed the complete number which is possible with:
(\d*\.\d{2})
which returns 308.56
But then is saw the line:
Is there any way to prevent this string from being extracted? Which made me expect you want only two numbers, a dot and again two numbers. Otherwise the regex should return nothing.
Then the answer should be:
(?<![\w\d])(\d{2}\.\d{2})(?![\w\d])
You can test it on https://regex101.com/
Upvotes: 1