Python中有没有一种方法可以进行if重新匹配&;组捕获在同一行中
在PERL中,我会这样做:
my $line = "abcdef";
if ($line =~ m/ab(.*)ef/) {
print "$1\n";
}
输出:
badger@pi0: scripts $ ./match.py
cd
但我在Python中能找到的最接近的方法是:
import re
line = 'abcdef'
if re.search('ab.*ef', line):
match = re.findall('ab(.*)ef', line)
print(match[0])
输出:
badger@pi0: scripts $ ./match.pl
cd
这场比赛似乎要进行两次
只需删除
search
。你不需要它或者,如果您只对第一个匹配感兴趣,请删除
findall
:相关问题 更多 >
编程相关推荐