import re
text = 'to to to how are you a to to'
result = re.findall('to', text)
print(result)
正则表达式中的findall语句,可以找到字符串中所有的需要找到的内容,并且将它打印出来。
import re
text = 'to to too how are you a to to gongsunli luban'
result = re.findall('lu...', text)
print(result)
正则表达式中的模糊匹配,点可以用来表示任意字符。
import re
text = 'to to too how are you a to to gongsunli luban gongbenwuzang, wangzhe wahah,'
result = re.findall('to[a-z]', text)
print(result)
匹配必须是字母的情况, 例子:写法是[a-z]
import re
text = 'to to too how are you a to to gongsunli luban gongbenwuzang, wangzhe wahah,'
result = re.findall(' (to[a-z]) ', text)
print(result)
在正则表达式中,加上括号可以表示,不需要将单词左右两边的空格打印输出到屏幕上的情况
import re
text = 'to to too how are you a too to gongsunli luban gongbenwuzang, wangzhe wahah,'
result = re.findall(' to[a-z] ', text)
result = set(result) # 去掉结果中重复的部分
print(result)
在正则表达式中,可以通过set语句将结果中重复的部分去掉。
import re
text = 'to to too how are you a too toooooooo gongsunli luban gongbenwuzang, wangzhe wahah,'
result = re.findall('to*', text)
print(result)
a* 可以匹配到 ’ ’ ‘a’ ‘aa’ ‘aaaa’ ‘aaaaaaaaaaaaaaaaaaaa’
import re
text = 'to to a1 too 456 153245 how are you a too toooooooo gongsunli luban gongbenwuzang, wangzhe wahah,'
result = re.findall('\d', text)
print(result)
\d表示匹配数字,且仅匹配一个数字
import re
text = 'to to a1 too 456 153245 how are you a too toooooooo gongsunli luban gongbenwuzang, wangzhe wahah,'
result = re.findall('\d+', text)
print(result)
\d+匹配在一起的数字