jonalv
jonalv

Reputation: 6116

Why is \d+ only matching one digit in this python regexp?

Regexp: editClassification/(?P<pk>[\d+])

String to match: foo/editClassification/10

pythex example

Upvotes: 0

Views: 536

Answers (2)

Kaspar Lee
Kaspar Lee

Reputation: 5596

Your \d+ was inside a character class (More info can be found at Regexp Tutorial or the PHP Docs). This means any letter inside is selected. For example, (a|b|c) is equivalent to the character class [abc]. So your character class was matching either one digit, or a +.

You should remove the [square brackets] around the \d+. Your new RegEx:

editClassification/(?P<pk>\d+)

Alternatively, you could just move the + outside the character class, but that just wastes space.

Live Demo on Pythex

Upvotes: 1

Because \d+ is within a character class ([...]); [\d+] matches exactly one character that is either a digit or +.

You were supposed to write (?P<pk>\d+) instead.

Upvotes: 2

Related Questions