繁体   English   中英

用Python regex匹配斜杠

[英]match trailing slash with 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/" =~ /\/$/'

如何解释这种行为?

re.match从字符串的开头搜索您的模式。 由于您的字符串不是以'/' re.match('/$', u'https://xxx/') ,因此re.match('/$', u'https://xxx/')返回任何内容。

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

暂无
暂无

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

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