I'm trying to find a way to write regex which match any 5 digit numbers in string, except those, which are followed by slash.
I can't figure out why this RE doesn't work:
r"\D(\d{%d})[^/]\D" % 5
EXAMPLE
'dsadasd894665' -> NO MATCH
'dsadsa78954,4' -> 78954
'dsda78954/sdd' -> NO MATCH
Have you any idea?
(?<!\d)(\d{5})(?!\/|\d)
Try this.This Works.See demo.Your regex does not start or end with 0 width assertions.So it actually needs 1 charcter before and 2 ahead.
You could try the below regex,
(?<!\d)\d{5}(?=[^\d/]|$)
EXplanation:
(?<!\\d)
Match wouldn't be preceded by a digit. \\d{5}
Exactly 5 digits. (?=[^\\d/]|$)
Must be followed by a non-digit or /
or end of the line anchor. Code:
>>> import re
>>> s1 = "dsadasd894665"
>>> s2 = "dsadsa78954,4"
>>> s3 = "dsda78954/sdd"
>>> re.search(r'(?<!\d)\d{5}(?=[^\d/]|$)', s1)
>>> re.search(r'(?<!\d)\d{5}(?=[^\d/]|$)', s2)
<_sre.SRE_Match object at 0x7f0835855370>
>>> re.search(r'(?<!\d)\d{5}(?=[^\d/]|$)', s3)
The reason your regex is not working is due to the \\D
s. I don't know why you put them in. Another problem is that [^/]
will not match the end of the string, so use ?!
instead.
The following works:
r"(\d{%d})(?!\/)" % 5
Since you want to find numbers that are on their own, ie not preceeded by another number, not followed by another number, and also not followed by a slash, we are going to use negative look-aheads and look-behinds to ensure this:
(?<!\d)\d{5}(?!/|\d)
This will match a \\d{5}
if it is not preceeded by another \\d
(ie no number is before it), and it is also not followed by another \\d
or a /
.
Using that on your examples yields the desired results:
>>> for example in ('dsadasd894665', 'dsadsa78954,4', 'dsda78954/sdd'):
print(re.search(r'(?<!\d)\d{%d}(?!/|\d)' % 5, example))
None
<_sre.SRE_Match object; span=(6, 11), match='78954'>
None
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.