0

Why does the second regex ('\w*(\d*)\w{0,3}') not match in python?

>>> re.search(r'\w{0,3}(\d*)\w{0,3}', 'abc123456def').groups()
('123456',)
>>> re.search(r'\w*(\d*)\w{0,3}', 'abc123456def').groups()
('',)
>>> re.search(r'\w{0,3}(\d*)\w*', 'abc123456def').groups()
('123456',)

The string begins with 0 or more word characters, so it seems like it should match like the first and third regex patterns.

  • 4
    It *does* match. Otherwise you would have gotten an AttributeError when you tried to call `groups`. However, the `(\d*)` doesn't match the `123456`. You may want to look up what `\w` means, because you seem to be expecting `\w` to not match digits. – user2357112 supports Monica Aug 03 '17 at 18:10
  • `\w` matches the same as `[a-zA-Z0-9_]`. `\w*` gobbles up the whole string so there is nothing left for the remaining patterns. – Steven Rumbalski Aug 03 '17 at 18:13
  • I think this is closer to what you're looking for: `re.search(r'\D*(\d*)\D{0,3}', 'abc123456def').groups()` Or remove the last `\D` entirely – Cory Madden Aug 03 '17 at 18:20
  • I misinterpreted the result of ('',) as being no match. This makes sense that it was a greedy match now. Thank you, @user2357112, @@StevenRumbalski and @@CoryMadden – ardk4855 Aug 03 '17 at 18:52

0 Answers0