tags:

views:

105

answers:

1

How do you 'break out' of a regex in Django?

Therefore if one of your URL tokens (after and before a slash /token/) needs 3 digits, a COLON, 3 letters, a 'T' and then 2 digits, a DASH and then 3 digits - how would you do this?

Example:

Accept: 678:bhgT23-789

Reject: 345:fdsM43-432

I've started off with a r'^(?P<somevar>\w{2})/', but don't know where to go from here.

EDIT:

Could you please talk me through any regex you offer i.e. why you used that specific pattern and what each bit means. Thanks.

+2  A: 
\d{3}:[a-z]{3}T\d\d-\d{3}

3 digits, colon, 3 letters (if you need uppercase too, make it [a-zA-Z]), a T, two digits, a dash, and then 3 digits.

Amber
May I ask why you have specifically used a `\d\d` instead of `\d{2}`?
day_trader
Probably to illustrate two different ways of expressing repetitions.
thomask
Because `\d\d` is one less character, probably. There's no material difference.
Alan Moore
While there was a small component of what @thomask alludes to (providing examples), it's more just what I'm used to - since adding another `\d` is shorter than `{x}`, for 2 of a class I usually just repeat the class. But you can also use `\d{2}` if you prefer.
Amber
Thank you for clarifying. Most appreciated. Another question, suppose you have a URL token that can be one of two regexes... how do you specify this? For instance, a time that can be either `4567890` or `10-10-2010T00:00:00.000`.
day_trader
`(x|y)` matches either regex `x` or regex `y`.
Amber