python 正则 匹配指定的多个词以及索引

import re
A= '原因是CPU电路板故障,需要CPU电路板进行更换。'
B='CPU电路板|原因'
for i in re.finditer(B, A):
    print(i.group()+str(i.span()))

打印结果:
原因(0, 2) CPU电路板(3, 9) CPU电路板(14, 20)

猜你喜欢

转载自www.cnblogs.com/chenyaling/p/9453714.html
今日推荐