ringa_lee2017-04-18 10:01:51
匹配到第二个括号的时候 值是 mo.group(2)
mo.group(2)
mo.group(1)
没匹配到就 None
mo.group(1)
没匹配到就 None
了#🎜🎜#
def mark(mo):
# print(mo.group(1))
for i in range(1, 4):
match_result = mo.group(i)
if match_result is not None:
print(i)
return match_result
re_sub = re.sub(r'@(yangxg)|@(zengshao)|@(zmrenwu)', mark, '@yangxg @zengshao @zmrenwu')
print(re_sub)
黄舟2017-04-18 10:01:51
这样写或许比较好些?输出符合你的要求
def mark(mo):
print(mo.group(1))
return mo.group(1)[1:]
data = re.sub(r'(@\w+)', mark, '@yangxg @zengshao @zmrenwu')
print data