0%

python使用正则后向引用替换字符串

工作需要把 Mud makes my mom mad. 这句话带有m的加上颜色,或者把某些单词加上颜色
临时写了个脚本处理

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import re
import sys


# replace letter
#find = "m"
#str = "Mud makes my mom mad."

#replace key words
#find = "Mud|makes|mad"
#str = "Mud makes my mom mad."

# how to use
# python b.py "m" "Mud makes my mom mad."
# python b.py "mud|mess|mop|make|the|help" "Mud makes my mom mad."


find = sys.argv[1]
str = sys.argv[2]


result = re.sub(r'('+find+')', r'<color:#ff0000>\1</color>', str, 0, re.IGNORECASE)
print result
给作者打赏,可以加首页微信,咨询作者相关问题!