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


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

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.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.