正则表达式 re.findall 用法
来源:互联网 发布:php开发流程 编辑:程序博客网 时间:2024/05/18 03:24
http://www.cnblogs.com/xieshengsen/p/6727064.html
正则 re.findall 的简单用法(返回string中所有与pattern相匹配的全部字串,返回形式为数组)
语法:
1
findall(pattern, string, flags
=
0
)
import re
Python 正则表达式 re findall 方法能够以列表的形式返回能匹配的子串
# print (help(re.findall))
# print (dir(re.findall))
findall查找全部r标识代表后面是正则的语句
1
2
3
regular_v1
=
re.findall(r
"docs"
,
"https://docs.python.org/3/whatsnew/3.6.html"
)
print
(regular_v1)
# ['docs']
符号^表示匹配以https开头的的字符串返回,
1
2
3
regular_v2
=
re.findall(r
"^https"
,
"https://docs.python.org/3/whatsnew/3.6.html"
)
print
(regular_v2)
# ['https']
用$符号表示以html结尾的字符串返回,判断是否字符串结束的字符串
1
2
3
regular_v3
=
re.findall(r
"html$"
,
"https://docs.python.org/3/whatsnew/3.6.html"
)
print
(regular_v3)
# ['html']
# [...]匹配括号中的其中一个字符
1
2
3
regular_v4
=
re.findall(r
"[t,w]h"
,
"https://docs.python.org/3/whatsnew/3.6.html"
)
print
(regular_v4)
# ['th', 'wh']
“d”是正则语法规则用来匹配0到9之间的数返回列表
1
2
3
4
5
6
regular_v5
=
re.findall(r
"\d"
,
"https://docs.python.org/3/whatsnew/3.6.html"
)
regular_v6
=
re.findall(r
"\d\d\d"
,
"https://docs.python.org/3/whatsnew/3.6.html/1234"
)
print
(regular_v5)
# ['3', '3', '6']
print
(regular_v6)
# ['123']
小d表示取数字0-9,大D表示不要数字,也就是出了数字以外的内容返回
1
2
3
regular_v7
=
re.findall(r
"\D"
,
"https://docs.python.org/3/whatsnew/3.6.html"
)
print
(regular_v7)
# ['h', 't', 't', 'p', 's', ':', '/', '/', 'd', 'o', 'c', 's', '.', 'p', 'y', 't', 'h', 'o', 'n', '.', 'o', 'r', 'g', '/', '/', 'w', 'h', 'a', 't', 's', 'n', 'e', 'w', '/', '.', '.', 'h', 't', 'm', 'l']
“w”在正则里面代表匹配从小写a到z,大写A到Z,数字0到9
1
2
3
regular_v8
=
re.findall(r
"\w"
,
"https://docs.python.org/3/whatsnew/3.6.html"
)
print
(regular_v8)
#['h', 't', 't', 'p', 's', 'd', 'o', 'c', 's', 'p', 'y', 't', 'h', 'o', 'n', 'o', 'r', 'g', '3', 'w', 'h', 'a', 't', 's', 'n', 'e', 'w', '3', '6', 'h', 't', 'm', 'l']
“W”在正则里面代表匹配除了字母与数字以外的特殊符号
1
2
3
regular_v9
=
re.findall(r
"\W"
,
"https://docs.python.org/3/whatsnew/3.6.html"
)
print
(regular_v9)
# [':', '/', '/', '.', '.', '/', '/', '/', '.', '.']
阅读全文
0 0
- 正则表达式 re.findall 用法
- python 正则表达式re.findall
- python 正则表达式re.findall
- python 正则表达式 之re.findall
- python正则表达式 re (二)findall与finditer
- 正则表达式二 :re.findall()和元字符
- Python正则表达式学习(5)——re.findall()
- Python中正则表达式re模块-compile()和findall()
- python正则表达式-findall
- python 正则表达式 () findall
- python3 正则匹配 re.split,re.finditer,re.findall 方法
- 正则表达式之match search findall finditer的用法
- python正则表达式re模块用法详解
- 正则表达式re.split()用法
- 【正则表达式】python re模块用法总结
- python re 模块 findall 函数用法简述
- python re 模块 findall 函数用法简述
- python re 模块 findall 函数用法简述
- Struts2学习总结(十一):Struts2的CRUD
- 5-30 字符串的冒泡排序 (20分)
- 视觉直观感受 7 种常用的排序算法
- 双色球中奖
- 入门级RxJava
- 正则表达式 re.findall 用法
- 目前普遍使用的框架是ssm框架,ssh框架基本是以前的项目中用到
- 项目编译报错 未配置输出路径
- CodeForces
- CTreeCtrl基本使用
- Codevs 3304 水果姐逛水果街Ⅰ
- 高级程序员的自我修养
- C# ORM 泛型与反射的交叉应用
- 23种设计模式(4):建造者模式