Path for user management

The USER_PATH expression can fit the following paths:

  • /user/
  • /user/<id>, where <id> means group of digits
  • /user/<id>/, the same as the previous one, but with a trailing backslash

These cases can be handled with the "^/user/((?P<user_id>\d+?)/?)?$" regular expression. This expression is a bit complex. It includes two groups (one is nested) and some other strange characters. Let's have a closer look.

?P<name> is a grouping attribute that sets the name of the capturing group. Every group in brackets can be accessed by the regex::Captures object. Named groups can be accessed by names.

\d is a special expression that matches any digit. To specify that we have one or more digits, we should add the + symbol, which tells us how many repetitions it may have. The * symbol can also be added, which tells us that there are zero or more repetitions, but we haven't used this in our regular expression.

There are two groups. The first is nested with the name user_id. It must include digits only to be parsed to the UserId type. The second is an enclosing group that contains the optional trailing slash. This whole group is optional, meaning that the expression can include a /user/ path without any identifier.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.133.158.36