Off Topic: Slug regular expression

1,333 views
Skip to first unread message

cjl

unread,
Oct 31, 2007, 11:38:34 AM10/31/07
to Django users
I know next-to-nothing about regular expressions, so I apologize in
advance if my question is stupid. I am trying to match a slug field.
Is there any difference between:

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

Paul Rauch

unread,
Oct 31, 2007, 11:57:18 AM10/31/07
to django...@googlegroups.com

> --~--~---------~--~----~------------~-------~--~----~
> 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

signature.asc

Tim Chase

unread,
Oct 31, 2007, 12:17:29 PM10/31/07
to django...@googlegroups.com
>> Is there any difference between:
>>
>> r'^accounts/(?P<slug>[\w-]+)/$'
>>
>> and
>>
>> r'^accounts/(?P<slug>[-\w]+)/$'
>
> they match the same.

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


cjl

unread,
Oct 31, 2007, 1:23:35 PM10/31/07
to Django users
Thank you for the informative reply.

> 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

Jeremy Dunck

unread,
Oct 31, 2007, 1:34:31 PM10/31/07
to django...@googlegroups.com
On 10/31/07, cjl <cjl...@gmail.com> wrote:
...

> >
> > 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.
>

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/

Kenneth Gonsalves

unread,
Oct 31, 2007, 10:07:09 PM10/31/07
to django...@googlegroups.com

On 31-Oct-07, at 9:08 PM, cjl wrote:

> 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/


Kristinn Örn Sigurðsson

unread,
Oct 31, 2007, 10:11:53 PM10/31/07
to django...@googlegroups.com
I'm just replying to the first post.

I would write it as follows:

r'^accounts/(?P<slug>[\w\-]+)/?$'

Kenneth Gonsalves

unread,
Oct 31, 2007, 10:33:33 PM10/31/07
to django...@googlegroups.com

On 01-Nov-07, at 7:41 AM, Kristinn Örn Sigurðsson wrote:

> r'^accounts/(?P<slug>[\w\-]+)/?$'

r'^accounts/(?P<slug>[\w-]+)/?$'

works for me - no need to escape the '-' if it is at the end

cjl

unread,
Oct 31, 2007, 11:07:44 PM10/31/07
to Django users
Thanks all.

What is the second '?' for...I don't have it in my regular expression,
and it seems to be working fine.
-cjl

Kristinn Örn Sigurðsson

unread,
Oct 31, 2007, 11:16:05 PM10/31/07
to django...@googlegroups.com
It says the last slash is optional.

cjl

unread,
Nov 1, 2007, 6:46:00 AM11/1/07
to Django users
Do I need it if Django is already appending slashes?

Jeremy Dunck

unread,
Nov 1, 2007, 10:03:50 AM11/1/07
to django...@googlegroups.com
On 11/1/07, cjl <cjl...@gmail.com> wrote:
>
> Do I need it if Django is already appending slashes?
>

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'.
"

Reply all
Reply to author
Forward
0 new messages