如何使用正则表达式在以下字符串中找到所有出现的单词"good "
f = "good and obj is a \good to look for it is good"
答案 0 :(得分:0)
import re
regex = re.compile("good")
regex.findall("good and obj is a \good to look for it is good")
['good', 'good', 'good']
答案 1 :(得分:0)
f = r"good and obj is a \good to look for it is good"
m = re.findall('good ', f)
会给你:
['good ', 'good ']
在定义f时应该使用原始字符串,除非\
应该与g
一起使用
答案 2 :(得分:0)
如果你想找到所有'好'的话
re.findll('good', f)
会奏效。 其他 你可以试试这个
import re
s = "good and obj is a \good to look for it is good"
s = re.sub(r'[^\/\s]good', '', s)
print re.findall(r'good', s)