Python中正则表达式re模块-compile()和findall()
来源:互联网 发布:上海广通网络 编辑:程序博客网 时间:2024/05/21 16:27
正则表达式中字符含义:
使用正则表达式来匹配字符:
匹配单个字符:
当正则表达式中不带括号时,列表的元素为字符串,此字符串为整个正则表达式匹配的内容
import restr="this is an example"re1=re.compile('\w')re1.findall(str)['t', 'h', 'i', 's', 'i', 's', 'a', 'n', 'e', 'x', 'a', 'm', 'p', 'l', 'e']
匹配单词:
re1=re.compile('\w+') #不带空格re1.findall(str)['this', 'is', 'an', 'example']
re1=re.compile('\w+\s') #带有空格re1.findall(str)['this ', 'is ', 'an ']
re1=re.compile('\w+\s+\w+') #字符空格字符re1.findall(str)['this is', 'an example']
当正则表达式中带有一个括号时,列表的元素为字符串,次字符串的内容与括号中的正则表达式相对应(不是整个表达式的匹配内容)
re1=re.compile('(\w+)\s+\w+')re1.findall(str)['this', 'an']
当正则表达式中带有多个括号时,列表的元素为多个字符串组成的tuple,tuple中的字符串个数与括号对数相同,字符串内容与每个括号内的正则表达式相对应,并且排放顺序是按括号出现的顺序。
re1=re.compile('((\w+)\s+\w+)') re1.findall(str)[('this is', 'this'), ('an example', 'an')]
参考博客:http://blog.csdn.net/drdairen/article/details/51134816[感谢博主]
阅读全文
0 0
- Python中正则表达式re模块-compile()和findall()
- Python 正则re模块之compile()和findall()详解
- python re.compile()和findall()
- python 正则表达式re.findall
- python 正则表达式re.findall
- python 正则表达式 之re.findall
- python中正则表达式与re 模块
- python中正则表达式与re 模块
- Python中re(正则表达式)模块学习
- Python中re(正则表达式)模块学习
- Python中re(正则表达式)模块学习
- Python中re(正则表达式)模块学习
- Python中re(正则表达式)模块学习
- Python中re(正则表达式)模块学习
- Python中re(正则表达式)模块学习
- Python中re(正则表达式)模块学习
- Python中re(正则表达式)模块学习
- Python中re(正则表达式)模块学习
- 如何利用树莓派打造一款机器人 | 新知 · 科技
- Shopping+在线购物商城的实现(Spring+SpringMVC+Hibernate)(附源码)
- 7月4日云栖精选夜读:从《网安法》出发_给企业安全管理者的五条建议
- php函---get_magic_quotes_gpc()与addslashes()
- Linux 其他用户添加sudo权限
- Python中正则表达式re模块-compile()和findall()
- Android 沉浸式状态栏与隐藏导航栏
- Android drawText获取text宽度的三种方式
- 一个高度使用百度地图的App开发实践总结
- git 相关问题
- Java中的static和volatile
- GetLastError()返回值列表
- 10. Regular Expression Matching
- Listview多条目加载