python中正则表达式
来源:互联网 发布:淘宝客服基础培训知识 编辑:程序博客网 时间:2024/05/25 19:56
正则表达式语法
python中正则表达式语法与linux中的兼容
查看正则表达式
re模块
Python提供re模块,包含所有正则表达式的功能。由于Python的字符串本身也用\转义,所以要特别注意:
s = 'ABC\\-001' # Python的字符串# 对应的正则表达式字符串变成:# 'ABC\-001'
建议使用Python的r前缀,就不用考虑转义的问题了:
s = r'ABC\-001' # Python的字符串# 对应的正则表达式字符串不变:# 'ABC\-001'
用match()
方法判断是否匹配成功,成功则返回Match
对象,否则返回None
test = '用户输入的字符串'if re.match(r'正则表达式', test): print('ok')else: print('failed')
切分字符串
用正则表达式切分字符串比用固定的字符更灵活,请看正常的切分代码:
>>> 'a b c'.split(' ')['a', 'b', '', '', 'c']
无法识别连续的空格,用正则表达式试试:
>>> re.split(r'\s+', 'a b c')['a', 'b', 'c']
无论多少个空格都可以正常分割。加入,
试试:
>>> re.split(r'[\s\,]+', 'a,b, c d')['a', 'b', 'c', 'd']
再加入;
试试:
>>> re.split(r'[\s\,\;]+', 'a,b;; c d')['a', 'b', 'c', 'd']
0 0
- python中正则表达式
- python中正则表达式
- Python中正则表达式
- python中正则表达式
- Python中有关正则表达式
- Python中使用正则表达式
- Python中使用正则表达式
- Python中正则表达式用法
- python中使用 正则表达式
- Python中使用正则表达式
- Python中使用正则表达式
- Python中使用正则表达式
- Python中正则表达式应用
- python中正则表达式详解
- python正则表达式中group
- python中常用正则表达式
- Python中正则表达式讲解
- 在python 中使用正则表达式
- retrofit2拦截器和请求封装
- 【Leetcode】:238. Product of Array Except Self 问题 in JAVA
- KMP算法学习文档
- 秒杀系统架构分析与实战
- Spring事务管理
- python中正则表达式
- 《采桑子》
- [线段树]分数修改
- vim命令
- Android中调用WebService服务的主要步骤
- C99标准之前没有bool类型(C99提供)
- 原生JS实现的简单“瀑布流”布局
- tomcat重新启动后,session不失效
- Spring IOC核心源码学习