Python Forum
re does not seem to distinguish \d and \w properly
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
re does not seem to distinguish \d and \w properly
#1
Why doesn't re distinguish between \d and \w here?

import re

s1 = '123EFG 123EHI 123EJK'

k = re.compile(r'3E')
k.search(s1) # returns <re.Match object; span=(2, 4), match='3E'>
k.findall(s1) # ['3E', '3E', '3E']

l = re.compile(r'\d\w')
l.search(s1) # returns <re.Match object; span=(0, 2), match='12'>
l.findall(s1) # returns ['12', '3E', '12', '3E', '12', '3E']

m= re.compile(r'\wE')
m.search(s1) # returns <re.Match object; span=(2, 4), match='3E'>
m.findall(s1) # returns ['3E', '3E', '3E']

n= re.compile(r'\d\d')
n.search(s1) # returns <re.Match object; span=(0, 2), match='12'>
n.findall(s1) # returns ['12', '12', '12']
Reply
#2
Try this regex:
regex = r"\d[A-Z]"
\w Matches any letter, digit or underscore. Equivalent to [a-zA-Z0-9_].
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#3
\d matches digits. \w matches upper and lower case letters and digits.
Reply
#4
Details on what \w and \d matches is explained in the Python documentation of the re module, see https://docs.python.org/3/library/re.htm...ion-syntax . If you want to match characters only excluding digits, take a look at \D.

Regards, noisefloor
Reply
#5
Thank you all!

Aha, I did not expect \w to match digits! I thought maybe it was a problem when using Idle! Live and learn!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [fixed] distinguish file name from the path paul18fr 5 126 Feb-13-2026, 02:49 PM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020