python 正则表达式re.findall
来源:互联网 发布:福建广电网络 编辑:程序博客网 时间:2024/05/18 03:37
python 正则表达式 re.findall 方法能够以列表的形式返回能匹配的子串。
re.findall(pattern, string[, flags]):
pattern 表示输入的正则表达式,搜索string,以列表形式返回全部能匹配的子串。先看个简单的代码:
import re
p = re.compile(r'\d+')
print p.findall('one1two2three3four4')
### output ###
# ['1', '2', '3', '4']
稍微复杂点比如:
info = '<a href="http://www.baidu.com">baidu</a>' 我们的需求是通过正则表达式提取网址和锚文本,那可以用到
findall()
relink = '<a href="(.*)">(.*)</a>'
info = '<a href="http://www.baidu.com">baidu</a>'
cinfo = re.findall(relink,info)
print(cinfo)
输出的结果:[('http://www.baidu.com', 'baidu')] 返回的是一个列表,列表里面是匹配的结果形成的元组形式。如果你需要用正则替换的话,可以看下python re sub
re.findall(pattern, string[, flags]):
pattern 表示输入的正则表达式,搜索string,以列表形式返回全部能匹配的子串。先看个简单的代码:
import re
p = re.compile(r'\d+')
print p.findall('one1two2three3four4')
### output ###
# ['1', '2', '3', '4']
稍微复杂点比如:
info = '<a href="http://www.baidu.com">baidu</a>' 我们的需求是通过正则表达式提取网址和锚文本,那可以用到
findall()
relink = '<a href="(.*)">(.*)</a>'
info = '<a href="http://www.baidu.com">baidu</a>'
cinfo = re.findall(relink,info)
print(cinfo)
输出的结果:[('http://www.baidu.com', 'baidu')] 返回的是一个列表,列表里面是匹配的结果形成的元组形式。如果你需要用正则替换的话,可以看下python re sub
- python 正则表达式re.findall
- python 正则表达式re.findall
- python 正则表达式 之re.findall
- 正则表达式 re.findall 用法
- python正则表达式 re (二)findall与finditer
- Python正则表达式学习(5)——re.findall()
- Python中正则表达式re模块-compile()和findall()
- python正则表达式-findall
- python 正则表达式 () findall
- 正则表达式二 :re.findall()和元字符
- Python 正则表达式 re
- python-re【正则表达式】
- python re 正则表达式
- Python re正则表达式
- python 正则表达式 re
- Python正则表达式(re)
- re 正则表达式 Python
- python正则表达式(re)
- Hadoop的MapReduce分布式计算
- POJ 1753
- 第一章、主线程与子线程
- 909422229__2、希尔排序(也称最小增量排序)程序员必须知道的8大排序和3大查找
- RS-485收发的零延时自动转换电路(转)
- python 正则表达式re.findall
- LIGHT OJ 1136 - Division by 3【找规律】
- 伸展树(Splay tree)学习小结
- 第二章、解析资源文件
- enctype="multipart/form-data"
- 归并排序
- 909422229__3、简单选择排序
- SystemCallStub与KiFastSystemCall的关系
- openstack学习资源与学习路线