matches the second bracket, the value is mo.group(2)
mo.group(1) 没匹配到就 None Got it
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)
When
matches the second bracket, the value is
mo.group(2)
mo.group(1)
没匹配到就None
Got itMaybe it’s better to write like this? The output meets your requirements