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']
本文深入探讨了正则表达式的应用,从re.compile到re.sub,详细解析了如何使用正则表达式进行字符串处理,包括替换、匹配中文、数字等,并通过实例展示了正则在字符串分割中的巧妙运用。

被折叠的 条评论
为什么被折叠?



