Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python regex - what does - (dash) mean

Tags:

python

regex

I know it can denote range, but for example here [-.\d] seems like this means decimal number. What does dash symbol in front of the regex mean? Also, why apart from [], are there () around them? What do the () mean?

like image 359
Trup Avatar asked Aug 10 '11 15:08

Trup


2 Answers

[-.\d] finds one character that is either ([]) a dash (-), a period (.) or a number (\d).

The parentheses around mean grouping, so that the matched value can later be accessed using the group() method of the Match object.

See also the documentation of the re module.

like image 132
Ferdinand Beyer Avatar answered Sep 21 '22 21:09

Ferdinand Beyer


The - doesn't mean anything special here, it is literally matching a dash (probably looking for dash used as a minus sign). The . is also literal -- it will match a dot (probably used for a decimal point). The \d will match any number (0-9). If you add a * or + multiplier to the end of the example you give it will match any number: positive, negative, or floating point.

The () parentheses that are used mark the start and end of a group and the contents of a group can be retrieved after a match has been performed.

See http://docs.python.org/library/re.html for more information.

like image 21
Lee Netherton Avatar answered Sep 19 '22 21:09

Lee Netherton