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.
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
Alternately, make the string a "raw" one (note the r before the
quote):
r'[-\w]+'
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
r'(-|\w)+'
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