Why should \D mean 1 or more non-digits? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Why should \D mean 1 or more non-digits?

Why should \D mean 1 or more non-digits? I expected \D to mean just a SINGLE non-digit instead. Since \d means a digit, doesn't it make sense that its inverse, namely /D, means A non-digit? NOT multiple of them, just a SINGLE non-digit.

29th Aug 2016, 5:20 PM
Krishna Limani
Krishna Limani - avatar
6 Answers
+ 3
1 or more , it depend on follow by +?* if + it means at least 1 or more if * it means at least 0 or more if ? it means don't care , can have one or more or none of it
31st Aug 2016, 5:09 PM
beauty1234
+ 2
See this example. import re pattern = r"(\D+\d)" match = re.match(pattern, "Hi 999!") if match: print("Match 1") match = re.match(pattern, "1, 23, 456!") if match: print("Match 2") match = re.match(pattern, " ! $?") if match: print("Match 3") You can try rubning it. The output is Match1. This means \D matches non-digits while \d matches digits.
31st Aug 2016, 4:55 AM
Aman Kothari
Aman Kothari - avatar
+ 2
\d means it is a digit [0-9] and \D is no digit [^0-9]
2nd Sep 2016, 3:30 PM
Sunil M E
+ 1
You are correct. "\D" matches exactly one non-digit character, just as "\d" matches exactly one digit. As others have pointed out, if you want to match one _or_more_ non-digit characters, you have to add a "+" on the end of it, as in "\D+".
10th Oct 2016, 11:31 PM
Brian Gerard
+ 1
\d matches digits i.e. 1,2,... even 1000000 and so on that's why it's inverse \D matches more than 1 non digits
22nd Jul 2018, 3:35 AM
Dare$devil
Dare$devil - avatar
+ 1
beauty1234, correction: ? means 0 or 1 times, but not more.
30th May 2021, 3:34 AM
#0009e7 [get]
#0009e7 [get] - avatar