Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is (\d+)/(\d+) in regex?

Tags:

I know it's a Regular Expression. I have seen this particular regular expression in a piece of code. What does it do? Thanks

like image 262
user1881957 Avatar asked Dec 24 '12 04:12

user1881957


People also ask

What does \d do in regex?

\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).

What does the metacharacter \d means in regular expression?

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.

What does backslash D mean in regex?

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.

What does D mean in regex Java?

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).


2 Answers

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 slash

Here 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',) 
like image 119
Burhan Khalid Avatar answered Sep 19 '22 21:09

Burhan Khalid


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.

like image 29
Ry- Avatar answered Sep 20 '22 21:09

Ry-