匹配Python正则表达式的尾部斜杠

时间:2014-08-01 06:52:03

标签: python regex

我尝试匹配尾随/像这样:

type(re.match('/$', u'https://x.x.x/'))
<type 'NoneType'>

然而,这个确实匹配:

type(re.match('.*/$', u'https://x.x.x/'))
<type '_sre.SRE_Match'>

使用Perl,第一个模式匹配:

perl -e 'print "true" if "https://example.org/" =~ /\/$/'

如何解释这种行为?

1 个答案:

答案 0 :(得分:6)

re.match从你的字符串开头搜索你的模式。由于您的字符串不以&#39; /&#39;开头,re.match('/$', u'https://x.x.x/')不返回任何内容。

您需要使用re.search('/$', u'https://x.x.x/')来查找上一个斜杠。