I know it's a Regular Expression. I have seen this particular regular expression in a piece of code. What does it do? Thanks
\d (digit) matches any single digit (same as [0-9] ). The uppercase counterpart \D (non-digit) matches any single character that is not a digit (same as [^0-9] ). \s (space) matches any single whitespace (same as [ \t\n\r\f] , blank, tab, newline, carriage-return and form-feed).
The RegExp \D Metacharacter in JavaScript is used to search non digit characters i.e all the characters except digits. It is same as [^0-9]. Syntax: /\D/ or new RegExp("\\D") Syntax with modifiers: /\D/g.
Within a regex a \ has a special meaning, e.g. \d means a decimal digit. If you add a backslash in front of the backslash this special meaning gets lost.
For example, \d means a range of digits (0-9), and \w means a word character (any lowercase letter, any uppercase letter, the underscore character, or any digit).
Expanding on minitech's answer:
(
start a capture group\d
a shorthand character class, which matches all numbers; it is the same as [0-9]
+
one or more of the expression)
end a capture group/
a literal forward slashHere is an example:
>>> import re >>> exp = re.compile('(\d+)/(\d+)') >>> foo = re.match(exp,'1234/5678') >>> foo.groups() ('1234', '5678')
If you remove the brackets ()
, the expression will still match, but you'll only capture one set:
>>> foo = re.match('\d+/(\d+)','1234/5678') >>> foo.groups() ('5678',)
It matches one or more digits followed by a slash followed by one or more digits.
The two "one or more digits" here also form groups, which can be extracted and used.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With