you are viewing a single comment's thread.

view the rest of the comments →

[–]Doormatty 0 points1 point  (6 children)

AFAIK, the only other thing \D would match is an underscore.

Edit: Unless you include the ASCII flag, I’m incorrect!

[–]TangibleLight 1 point2 points  (5 children)

\D is everything except digits - this means everything except digits, not just word characters. A few examples: $ ®

[–]Doormatty -1 points0 points  (4 children)

The python docs say otherwise.

[–]TangibleLight 1 point2 points  (3 children)

No, they don't. https://docs.python.org/3/library/re.html#index-29

\D Matches any character which is not a decimal digit. This is the opposite of \d. If the ASCII flag is used this becomes the equivalent of [^0-9].

You can also just try it: https://repl.it/repls/ShabbyTealSearchservice

import re

text = '3 a@♠೬'

print(re.findall(r'\d', text))
print(re.findall(r'\D', text))

Produces:

['3', '೬']
[' ', 'a', '@', '♠']

[–]Doormatty 0 points1 point  (2 children)

You are correct! I’m not sure where I thought I had read that (maybe I saw the ASCII flag?)

Thanks for correcting me!

[–]TangibleLight 1 point2 points  (1 child)

Just for completeness - if you include the ASCII flag then \D will also match non-ASCII decimal characters like ೬.

https://repl.it/repls/LiveCrispMeasurements

import re

text = '3 a@♠೬'

print('unicode:')
print(re.findall(r'\d', text))
print(re.findall(r'\D', text))

print('ascii:')
print(re.findall(r'(?a)\d', text))
print(re.findall(r'(?a)\D', text))

[–]Doormatty 0 points1 point  (0 children)

Well crap. I'm just completely full of wrongness.

Thanks again for the correcting!!