matching a hyphen '-' character in urls

1,032 views
Skip to first unread message

hotani

unread,
Jun 14, 2007, 1:36:54 PM6/14/07
to Django users
Having issues matching a hyphen character in my urls file. Here is an
example of what needs to be passed: "12-34-56".

In python, the "\w+" matches this, but django urls will not. I have
also tried (from another post in this forum) "\[-w]+" which did not
match it either. The only way I have been able to get django to match
this string is with a wildcard like so: ".+". I'm sure there is a
better way, but I'm not seeing it.

Malcolm Tredinnick

unread,
Jun 14, 2007, 6:07:47 PM6/14/07
to django...@googlegroups.com
On Thu, 2007-06-14 at 17:36 +0000, hotani wrote:
> Having issues matching a hyphen character in my urls file. Here is an
> example of what needs to be passed: "12-34-56".
>
> In python, the "\w+" matches this, but django urls will not.

Since Django's URL pattern matching is done using Python reg-exps, this
can't be correct. In fact, \w does not match a hyphen in Python (it
matches alphanumeric and underscore only).

What you want is "[-A-Za-z0-9_]+".

The reason [-\w] doesn't work is because inside character classes
([...]), \w no longer means "alphanumeric characters". It's just a
character escape.

Regards,
Malcolm


SmileyChris

unread,
Jun 14, 2007, 6:13:32 PM6/14/07
to Django users
> What you want is "[-A-Za-z0-9_]+".
>
> The reason [-\w] doesn't work is because inside character classes
> ([...]), \w no longer means "alphanumeric characters". It's just a
> character escape.

Alternately, make the string a "raw" one (note the r before the
quote):
r'[-\w]+'

Forest Bond

unread,
Jun 14, 2007, 6:24:49 PM6/14/07
to django...@googlegroups.com

Not sure that's true. Malcolm was indicating that it is a character escape at
the reg-exp level, not the Python string-parsing level.

Character classes can't be merged in Python reg-exps. \w represents an entire
character class, and, consequently, can't be merged with other classes by
putting \w inside the new character class.

-Forest

signature.asc

Jeremy Dunck

unread,
Jun 14, 2007, 6:30:33 PM6/14/07
to django...@googlegroups.com
On 6/14/07, SmileyChris <smile...@gmail.com> wrote:
> Alternately, make the string a "raw" one (note the r before the
> quote):
> r'[-\w]+'

r'(-|\w)+'

Forest Bond

unread,
Jun 14, 2007, 6:34:27 PM6/14/07
to django...@googlegroups.com

Hey, hadn't thought about that, but I like it!

-F

signature.asc

Malcolm Tredinnick

unread,
Jun 14, 2007, 6:59:51 PM6/14/07
to django...@googlegroups.com

Chris is right; I am clearly on drugs (again).

In [6]: re.match(r'[-\w]+', '12-34-56').group()
Out[6]: '12-34-56'

That was what I thought the answer was and then when I tested it before
making my reply it didn't work and the rationalisation seemed sound, so
I went with what worked. However, I must have made a typo when testing.

Regards,
Malcolm


Forest Bond

unread,
Jun 14, 2007, 7:11:34 PM6/14/07
to django...@googlegroups.com

Oh. Didn't know that you could do that, and, as a result, assumed you were
right. Sorry about that.

-Forest

signature.asc

hotani

unread,
Jun 14, 2007, 11:11:57 PM6/14/07
to Django users
Thanks for the comments. I'll post back tomorrow with the python
script I was using that worked (somehow?) for matching the hyphen with
a '\w+'. I'll also try these suggestions and post back the results.

Reply all
Reply to author
Forward
0 new messages