re.compile( ):将正则表达式编译成一个对象,加快速度并可以重复使用。
import re
re.complie()
re.sub() :对字符串做替换处理
'''
re.sub(pattern, repl, string, count=0, flags=0)
'''
test = "今天是我的18岁生日,我收到1份价值10万的礼物。"
pattren = re.compile('[\u4e00-\u9fa5]')
print(pattern.sub("", test))
output: 18,110。
常用的几个正则表达式:
\d | 匹配一个数字 |
---|---|
\d{3} | 匹配3个数字,如‘010’ |
\w | 匹配一个数字或者字母 |
[\u4e00-\u9fa5] | 匹配一个中文 |
^ | 行的开头,^\d 必须以数字开头 |
$ | 行的结尾 |
同时可以将正则使用在split中:
>>> test= 'a b c d'
>>> test.split(' ')
['a', 'b', '', '', '', 'c', '', 'd']
'''针对不规范的用户输入,巧用正则化'''
>>> test.split() #默认分隔符为所有空字符,包括换行符(\n),制表符(\t)。
['a', 'b', 'c', 'd']
>>> re.split("\s+",test)
['a', 'b', 'c', 'd']
>>> test= 'a b, c; d'
>>> re.split("[\s,;]+",test)
['a', 'b', 'c', 'd']