Does "\D" in Regex Mean a Digit

What does \d+ mean in a regular expression?

\d is a digit (a character in the range [0-9]), and + means one or more times. Thus, \d+ means match one or more digits.

For example, the string "42" is matched by the pattern \d+.


You can also find explanations for pieces of regular expressions like this using a tool like Regex101 (online, free) or Regex Coach (downloadable for Windows, free) that will let you enter a regular expression and sample text, then indicate what (if anything) matches the regex. They also try to explain, in words, what the regular expression does.

What does this (\(\d\d\d\)\s)? regex match?

The regex expression : Sample Image

The first backslash escapes the open parenthesis that follows, as it is a special character, so the regex will search for an open and a close parenthesis in the input string

Example : (111)

What does regular expression [A-Z]{2,3}\\d+ mean

[A-Z]{0,2,3}\\d+

That looks like a typo.

{} braces shouldn't have more than two numbers in it.

[A-Z]{0,3} mean any alphabet between A and Z (capital) can occur 0 to 3 times.

And \d+ means any digit (0-9) can occur 1 or more times.

That is the reason [A-Z]{0,3}\d+ matches AAA1234, 0000123 and AA12345

What does the regular expression [.\d\D]* mean?

Well [.\d\D]* means match:

0 or more of anyone of these properties

  • digit
  • non-digit
  • literal dot

IMO this is not really required since this can effectively match anything and is equivalent of .* with DOTALL switch.

Is there a difference between \d and \d+?

The g at the end means global, ie. that you want to search for all occurrences. Without it, you'll just get the first match.

\d, as you know, means a single digit. You can add quantifiers to specify whether you want to match all the following, or a certain amount of digits afterwards.

\d means a single digit

\d+ means all sequential digits

So let's say we have a string like this:

123 456
7890123

/\d/g will match [1,2,3,4,5,6,7,8,9,0,1,2,3]

/\d/ will match 1

/\d+/ will match 123

/\d+/g will match [123,456,7890123]

You could also use /\d{1,3}/g to say you want to match all occurrences where there are from 1 to 3 digits in a sequence.

Another common quantifier is the star symbol, which means 0 or more. For example /1\d*/g would match all sequences of digits that start with 1, and have 0 or more digits after it.

What is the difference between the regular expressions [^\d\s] and [\D\S]

[^\d\s]

will match a single character that is NOT a digit or whitespace.

[\D\S]

will match a single character that IS a non-digit or non-whitespace.

Since every character is either not a digit or not whitespace, the second regex will match any character.

It's equivalent to the difference between:

if (!(isdigit(c) || isspace(c))) ...

and

if (!isdigit(c) || !isspace(c)) ...

Note that the following would be equivalent to the first one (by deMorgan's law):

if (!isdigit(c) && !isspace(c)) ...

What does this REGEX means? [a-zA-Z]|\d

\d is a digit character. Your code means "any alphabetic or numeric character". It could more easily be expressed as [A-Za-z0-9].



Related Topics



Leave a reply



Submit