简体   繁体   English

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

[英]Regex matching alphanumeric with special characters

I am trying to form an url in Django url pattern . 我正在尝试在Django URL模式中形成一个URL。

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

The bu can be a string or string alphanumeric or alphanumeric with special character (except /) bu可以是字符串或字符串,字母数字或带有特殊字符的字母数字(/除外)

But the above url is showing error for 但是上面的URL显示错误

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

From the error I understood is this regex is not accepting the string having special character 从我理解的错误是这个正则表达式不接受具有特殊字符的字符串

Please help me out what might wrong here . 请帮我解决这里可能出什么问题。

Your current RegEx will match: 您当前的RegEx将匹配:

  • A string of one or more letters, numbers and/or underscores: [A-Za-z0-9_]+ 一串包含一个或多个字母,数字和/或下划线的字符串:[A-Za-z0-9 _] +

OR 要么

  • A SINGLE character that is NOT a letter, number or underscore. 不是字母,数字或下划线的单个字符。 [^A-Za-z0-9_] [^ A-ZA-Z0-9_]

You probably need something more like: 您可能需要更多类似的东西:

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

This will match letters, numbers, underscore and hyphens. 这将匹配字母,数字,下划线和连字符。 Add any other special characters you want as well (inside the square brackets). 还要添加其他所需的特殊字符(在方括号内)。 Remeber certain characters need escaping with \\ before them. 记住某些字符需要在它们之前使用\\进行转义。

Try this- 尝试这个-

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM