r'^accounts/(?P<slug>[\w-]+)/$'
and
r'^accounts/(?P<slug>[-\w]+)/$'
Will these two regular expressions match the same strings? The both
seem to work for me, but I couldn't find any information about the
importance of order between square brackets.
-cjl
> --~--~---------~--~----~------------~-------~--~----~
> You received this message because you are subscribed to the Google
> Groups "Django users" group. To post to this group, send email to
> django...@googlegroups.com To unsubscribe from this group, send
> email to django-users...@googlegroups.com For more options,
> visit this group at http://groups.google.com/group/django-users?hl=en
> -~----------~----~----~----~------~----~------~--~---
>
they match the same.
greetz Paul Rauch
To expand on Paul's answer, yes they're the same. The only time
order matters is detailed at [1] where the character-set "[...]"
notation is detailed. Namely it needs to be unambiguous to the
RE parse...to include a "-" or a "]" character in your
characterset, you can either place it as the first character, or
preceed it with a backslash (or otherwise make it unambiguous).
Thus, if you had a more limited characterset of, say,
vowels+hyphen, this regexp
'[aei-ou]'
is very different from
'[-aeiou]'
or
'[aeiou-]'
(the 2nd and 3rd are effectively the same)
As you learn regular expressions, you'll go through a couple phases:
1) what the #$%^ is all this line-noise?
2) regexps rock! they'll solve any problem I throw at them!
3) regexps rock! I now know what types of problems the solve
well without abusing them
Welcome to the transition between steps #1 and #2 ;)
-tim
[1] http://docs.python.org/lib/re-syntax.html
> As you learn regular expressions, you'll go through a couple phases:
>
> 1) what the #$%^ is all this line-noise?
>
> 2) regexps rock! they'll solve any problem I throw at them!
>
> 3) regexps rock! I now know what types of problems the solve
> well without abusing them
>
> Welcome to the transition between steps #1 and #2 ;)
Actually, I think I'm still at step 0.5, the one where I wish someone
would post a library of frequently used Django URLconf regular
expressions, so I could 'borrow' them.
-cjl
This is a pretty common response to regex's, but they really are very
useful. Well worth learning.
If you're having trouble figuring out what's going on, Kodos might help:
http://kodos.sourceforge.net/
> Will these two regular expressions match the same strings? The both
> seem to work for me, but I couldn't find any information about the
> importance of order between square brackets.
dont put '-' between any letters in square brackets - to be on the
safe side, put it last always
--
regards
kg
http://lawgon.livejournal.com
http://nrcfosshelpline.in/web/
> r'^accounts/(?P<slug>[\w\-]+)/?$'
r'^accounts/(?P<slug>[\w-]+)/?$'
works for me - no need to escape the '-' if it is at the end
What is the second '?' for...I don't have it in my regular expression,
and it seems to be working fine.
-cjl
No.
Writing regular expressions is obviously something that can be done
different ways. I think you should write your URLconfs expecting the
slashes to be there so that you let Django append it and therefore
have a single URL format. This gives you a little more coherence when
people link to you, and also helps with search ranking a little.
If you have APPEND_SLASH = False and wish to accept either form, then
you need to have the ? after the final "/" in order to match either
form.
The answer to what that last "?" did, though, is answered in the re
syntax guide which Tim posted.
( http://docs.python.org/lib/re-syntax.html )
Quoting:
"
"?"
Causes the resulting RE to match 0 or 1 repetitions of the
preceding RE. ab? will match either 'a' or 'ab'.
"