正则表达式将字母数字与特殊字符匹配

时间:2014-04-16 06:26:51

标签: python regex django

我正在尝试在Django url模式中形成一个URL。

   url(r'^reports/(?P<bu>\w+|\W)/$', \
                LrsReportView.as_view(), name="elearning.lrsreports.region"),  

bu可以是字符串或字符串字母数字或带有特殊字符的字母数字(除了/)

但上面的网址显示错误

NoReverseMatch: Reverse for 'elearning.lrsreports.region' with arguments '(u'Lawn&Garden',)' and keyword arguments '{}' not found.

从我理解的错误中,这个正则表达式不接受具有特殊字符的字符串

请帮我解决这里可能出现的问题。

2 个答案:

答案 0 :(得分:2)

您当前的RegEx将匹配:

  • 一个或多个字母,数字和/或下划线的字符串:[A-Za-z0-9 _] +

OR

  • 不是字母,数字或下划线的单个字符。 [^ A-ZA-Z0-9 _]

您可能需要更多类似的内容:

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

这将匹配字母,数字,下划线和连字符。添加您想要的任何其他特殊字符(在方括号内)。记住某些角色需要在\之前逃脱。

答案 1 :(得分:0)

试试这个 -

/ ^(= \ d?)(= (_(= [AZ]。)(= [AZ]?)|?[ ^ \ W]))。+ $ /

相关问题