我需要一个正则表达式来匹配以下模式:
/页/我的段塞/
/ A-langage-acronyme /页/我的段塞/
并且不符合以下模式:
/ FR /页/我的段塞/
/ EN /页/我的段塞/
我试过了:
r'^(?!(fr/|en/))page/(?P<slug>[\w-]+)/'
但它甚至不匹配:
/ DE /页/我的段塞/
答案 0 :(得分:0)
^/(?!fr|en)\w{2}/page/(?P<slug>[\w-]+)/
^ ## the start of the string
/ ## matches "/" character
(?!fr|en) ## should not match "fr" or "en"
\w{2} ## needs 2 words characters e.g. "de"
/page/ ## matches "/page/"
(?P<slug>[\w-]+) ## named capture group "slug" : matches one or more word and "-" characters
/ ## matches "/" character
<强>更新强>
^(?:/(?!fr|en)[\w-]+)?/page/(?P<slug>[\w-]+)/
匹配:
/page/my-slug/
/de/page/my-slug/
/de_DE/page/my-slug/
/de-BE/page/my-slug/
/deLU/page/my-slug/
不匹配:
/en/page/my-slug/
/enUS/page/my-slug/
/en_US/page/my-slug/
/en-US/page/my-slug/
/enUS/page/my-slug/
/fr/page/my-slug/
/fr_FR/page/my-slug/
答案 1 :(得分:0)
我个人会采取略微不同的方法:
import re
regex = "^/(?:fr|de)/"
mylist = [
"/fr/a-langage-acronyme/page/my-slug/",
"/en/a-langage-acronyme/page/my-slug/",
]
for text in mylist:
if not re.search(regex, text):
print ("do stuff with " + text)
else:
print ("those silly fr and de, nein!")
你可以制作一个非常好的正则表达式来做你想做的事情,但是如果你真的只想避免以特定事物开头的路径,那就找那些并且不要使用它们。只是另一种看待它的方式。有时易于阅读将为您节省时间:)