sinθ
sinθ

Reputation: 11493

Django: An explanation of (\d+)

In the django book, and on the django website, \d+ is used to capture data from the url. The syntax is never explain, nor is the importance of D or the () beyond the fact that you can specify the number of characters in that part of the url. How, exactly/in what order, are these variables passed to the function? Hoe, exactly, does the syntax work? How do you implement it? Don't forget to explain the ()

Upvotes: 2

Views: 6269

Answers (3)

Amber
Amber

Reputation: 527238

That is a regular expression. \d means digit, and + means "one or more". Putting it in parens specifies that it's a capturing group. The contents of each capturing group are passed to the handler function in the order they appear within the regex.

Python's regex library is re.

As another example, a more complex regex might be (\d+)/(\d+) which would capture two different sets of one or more digits separated by a slash, and pass them in as two arguments (the first digit string as the first argument, the second digit string as the second argument) to the handler function.

Upvotes: 6

Edmon
Edmon

Reputation: 4872

It refers to a digit regular expression used to capture the numeric ID from the URI.

Hi-REST conventions call for URI for GET/POST/... to be terminated by an ID of the resource, and in this cases it is looking for a numerical ID - \d+ being one or more numbers)

Enclosing it in parenthesis is simply a convention that assist Django in parsing the regex.

Example:

http://www.amazon.com/dp/0486653552

Upvotes: 1

BrenBarn
BrenBarn

Reputation: 251548

Without further information, I'd guess it's a regular expression (or "regex" for short). These are a common string-processing mechanism used in many programming languages. In Python they are handled with the re module. If you want to learn more about them you might want to check out a general regex tutorial like http://www.regular-expressions.info/.

Upvotes: 6

Related Questions