('/(.*)', ByName)#通过为我的相关位置进行自定义映射来处理/new_york实体,/sao_paulo实体等.由于我有其他处理程序,并且我不想与正则表达式冲突,并且我还有其他请求处理程序,因此,您能推荐一下如何编写正则表达式的方法吗?当表达式适合2个正则表达式时如何工作?哪个优先级更高?您能否告诉我更多信息,我应该如何学习如何为地理数据存储区编写正则表达式和可能的实现-作为实体或实例变量以及特殊问题(例如以不同语言使用不同名称的地理位置)德语中的德国称为Deutschland,所以我也想应用我可以使用gettext/djang.po文件进行的翻译. 解决方案首场比赛获胜.通常,您的URL在路径的其他部分会有所不同.例如,您可能有/cities/(?P<city>[^/]+)/users/(?P<user>[^/]+)在很多情况下,[^/] +是一个很好的正则表达式,因为它将匹配/以外的任何其他内容,通常会避免使用/,因为它用于分隔路径元素.我不认为仅根据字符(在您的情况下为字母或数字)来分隔URL是个好主意,但如果要这样做,请使用[-A-Za-z_]+(请注意,-"位于[]的开始,否则需要反斜杠.避免使用\w,因为它也可以匹配数字.除非您想发疯并且仅将数字发送给一个处理程序,而将数字和字母+数字发送给其他地方,在这种情况下,请使用:/(?P<id>\d+)/(?P<city>[-\w]+)依次.I want to handle geographic names i.e /new_york or /new-york etcand since new-york is django-slugify for New york then maybe I should use the slugifed names even if names with underscores look better since I may want to automate the URL creation via an algorithm such as django slugify. A guess is that ([A-Za-z]+) or simply ([\w-]+) can work but to be safe I ask you which regex is best choice in this case.I've already got a regex that handles number connecting numbers to a class:('/([0-9]*)',ById)#fetches and displays an entity by idNow I want another regex to match names e.g. new_york so that a request for/new_york gets handled by the appropriate handler. Basically the negation of the regex above would or any combination letters+underscore and maybe a dash - since the names are geographical and It seems I could use this regex but I believe it works only because of precedence it that it just takes everything:('/(.*)', ByName)#Handle for instance /new_york entities, /sao_paulo entities etc by custom mapping for my relevant places.Since I have other handlers and I don't want conflicting regexes and I have other request handlers, could you recommend how to formulate the regex?How does it work when an expression suits 2 regexes? Which has higher precedence? Can you tell me more how I should learn to write regexes and possible implementations for the geographical datastore - as entities or instance variables and special problems such as geographic locations that have different names in different languages e.g. Germany in german is called Deutschland so I also want to apply translations that I can do with gettext / djang.po files. 解决方案 the first match wins.usually your URLs will differ in other parts of the path. for example you might have/cities/(?P<city>[^/]+)/users/(?P<user>[^/]+)and in many cases [^/]+ is a good regex because it will match anything except /, which you would normally avoid because it is used to separate path elements.i don't think it's a good idea to separate URLs based solely on characters (in your case, letters or digits), but if you want to do that, use [-A-Za-z_]+ (note that the "-" goes at the start of the [], or it needs a backslash).avoid \w because that can also match digits. unless you want to go really crazy and send digits only to one handler and letters+digits elsewhere, in which case use:/(?P<id>\d+)/(?P<city>[-\w]+)in that order. 这篇关于我可以对字母,破折号和下划线使用python正则表达式吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!