[英]match python regex url exactly
I have the following: 我有以下内容:
rule = "http://www.abc.com/"
test = "http://www.abc.com/test"
print(str(re.compile(rule).match(test)))
I want this to output None but instead it returns a match. 我希望这输出无,但它返回一个匹配。 How can I change the rule variable so the regex returns None? 如何更改规则变量以使正则表达式返回None?
I do not think that you need regexp here if you wish to compare full strings. 如果你想比较完整的字符串,我认为你不需要regexp。 Please correct me if I misunderstand you. 如果我误解你,请纠正我。 :) :)
May be this code will be uesful: 可能这段代码很有用:
rule = "http://www.abc.com/"
test = "http://www.abc.com/test"
print(rule == test)
Returns False
if strings are different, True
otherwise. 如果字符串不同则返回False
,否则返回True
。
The ^
character matches the beginning of the string, and $
matches the end of the string. ^
字符匹配字符串的开头, $
匹配字符串的结尾。 So you'd want: 所以你想要:
rule = "^http://www\.abc\.com/$"
test = "http://www.abc.com/test"
print(str(re.compile(rule).match(test)))
Note that .
请注意.
means "match any character" so if you want to match an actual .
意味着“匹配任何字符”,所以如果你想匹配一个实际的.
you need the \\
before it. 你之前需要\\
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.