What does [- \ w] + mean in python regex?

urlpatterns = patterns('basic.blog.views',
    url(r'^(?P<year>\d{4})/(?P<month>\w{3})/(?P<day>\d{1,2})/(?P<slug>[-\w]+)/$',
        view='post_detail',
        name='blog_detail'
    ),

      

what matches [-\w]+

in (?P<slug>[-\w]+)

? Specifically, what do the dashes and square brackets match?

+3


source to share


2 answers


[-\w]

says that either a word symbol ( A-Za-z0-9_

) or a dash ( -

) can go there .



Here's a pretty good site that will tell you what exactly your regex does: http://www.regex101.com/r/cJ2zT8

+9


source


Square brackets create a "character class", which means "match any of the characters between those square brackets. In your case, that literal character -

or any word character is A-Z

, A-Z

0-9

or _

. +

In this case means" match one or more of the previous character class " This means that you are matching strings that contain one or more characters from the set A-Za-z0-9_-

.



0


source







All Articles