python-day05正则表达式
相关参考文档地址:http://bbs.fishc.com/thread-57073-1-1.html(小甲鱼论坛)
摘录老师之精华
re模块用于对python的正则表达式的操作。
字符:
. 匹配除换行符以外的任意字符
\w 匹配字母或数字或下划线或汉字
\s 匹配任意的空白符
\d 匹配数字
\b 匹配单词的开始或结束
^ 匹配字符串的开始
$ 匹配字符串的结束
次数:
* 重复零次或更多次
+ 重复一次或更多次
? 重复零次或一次
{n} 重复n次
{n,} 重复n次或更多次
{n,m} 重复n到m次
IP: ^(25[0-5]|2[0-4]\d|[0-1]?\d?\d)(\.(25[0-5]|2[0-4]\d|[0-1]?\d?\d)){3}$ 手机号: ^1[3|4|5|8][0-9]\d{8}$
1、match(pattern, string, flags=0)
从起始位置开始根据模型去字符串中匹配指定内容,匹配单个
- 正则表达式
- 要匹配的字符串
- 标志位,用于控制正则表达式的匹配方式
1 import re 2 3 obj = re.match('\d+', '123uuasf') 4 if obj: 5 print obj.group()
View Code
1 # flags 2 I = IGNORECASE = sre_compile.SRE_FLAG_IGNORECASE # ignore case 3 L = LOCALE = sre_compile.SRE_FLAG_LOCALE # assume current 8-bit locale 4 U = UNICODE = sre_compile.SRE_FLAG_UNICODE # assume unicode locale 5 M = MULTILINE = sre_compile.SRE_FLAG_MULTILINE # make anchors look for newline 6 S = DOTALL = sre_compile.SRE_FLAG_DOTALL # make dot match newline 7 X = VERBOSE = sre_compile.SRE_FLAG_VERBOSE # ignore whitespace and comments
2、search(pattern, string, flags=0)
根据模型去字符串中匹配指定内容,匹配单个
1 import re 2 3 obj = re.search('\d+', 'u123uu888asf') 4 if obj: 5 print obj.group()
View Code
3、group和groups
1 a = "123abc456" 2 print re.search("([0-9]*)([a-z]*)([0-9]*)", a).group() 3 4 print re.search("([0-9]*)([a-z]*)([0-9]*)", a).group(0) 5 print re.search("([0-9]*)([a-z]*)([0-9]*)", a).group(1) 6 print re.search("([0-9]*)([a-z]*)([0-9]*)", a).group(2) 7 8 print re.search("([0-9]*)([a-z]*)([0-9]*)", a).groups()
View Code
4、findall(pattern, string, flags=0)
上述两中方式均用于匹配单值,即:只能匹配字符串中的一个,如果想要匹配到字符串中所有符合条件的元素,则需要使用 findall。
1 import re 2 3 obj = re.findall('\d+', 'fa123uu888asf') 4 print obj
View Code
5、sub(pattern, repl, string, count=0, flags=0)
用于替换匹配的字符串
content = "123abc456" new_content = re.sub('\d+', 'sb', content) # new_content = re.sub('\d+', 'sb', content, 1) print new_content
相比于str.replace功能更加强大
6、split(pattern, string, maxsplit=0, flags=0)
根据指定匹配进行分组
示例
1 content = "'1 - 2 * ((60-30+1*(9-2*5/3+7/3*99/4*2998+10*568/14))-(-4*3)/(16-3*2) )'" 2 new_content = re.split('\*', content) 3 # new_content = re.split('\*', content, 1) 4 print new_content 5 content = "'1 - 2 * ((60-30+1*(9-2*5/3+7/3*99/4*2998+10*568/14))-(-4*3)/(16-3*2) )'" 6 new_content = re.split('[\+\-\*\/]+', content) 7 # new_content = re.split('\*', content, 1) 8 print new_content 9 inpp = '1-2*((60-30 +(-40-5)*(9-2*5/3 + 7 /3*99/4*2998 +10 * 568/14 )) - (-4*3)/ (16-3*2))' 10 inpp = re.sub('\s*','',inpp) 11 new_content = re.split('\(([\+\-\*\/]?\d+[\+\-\*\/]?\d+){1}\)', inpp, 1) 12 print new_content
View Code
相比于str.split更加强大
转载于:https://www.cnblogs.com/237325670qqcom/p/5547693.html
python-day05正则表达式相关推荐
- Python中正则表达式用法 重点格式以这个为准_首看_各种问题
20210811 https://www.jb51.net/article/101258.htm 一.惰性模式的概念: 此模式和贪婪模式恰好相反,它尽可能少的匹配字符以满足正则表达式即可,例如: va ...
- python使用正则表达式判别字符串是否以一个大写字符起始而跟随了一些小写字符
python使用正则表达式判别字符串是否以一个大写字符起始而跟随了一些小写字符 # # Python3 code to find sequences of one upper # case lette ...
- python使用正则表达式统计字符串中出现次数最多的数字
python使用正则表达式统计字符串中出现次数最多的数字 #python使用正则表达式统计字符串中出现次数最多的数字 # find the most occurring element import ...
- python使用正则表达式识别大写字母并在大写字母前插入空格
python使用正则表达式识别大写字母并在大写字母前插入空格 #python使用正则表达式识别大写字母并在大写字母前插入空格 import redef putSpace(input):# regex ...
- python使用正则表达式删除字符串中的其它字符只保留数字和字母
python使用正则表达式删除字符串中的其它字符只保留数字和字母 #python使用正则表达式删除字符串中的其它字符只保留数字和字母 # Python code to demonstrate # to ...
- python使用正则表达式寻找具有特定后缀的文件
python使用正则表达式寻找具有特定后缀的文件 # python使用正则表达式寻找具有特定后缀的文件 # import library import re# list of different ty ...
- python使用正则表达式抽取字符串中最大数值数字
python使用正则表达式抽取字符串中最大数值数字 #python使用正则表达式抽取字符串中最大数值数字 # Function to extract maximum numeric value fro ...
- python使用正则表达式去除句子中的重复词
python使用正则表达式去除句子中的重复词 #python使用正则表达式去除句子中的重复词 # Python program to remove duplicate words # using Re ...
- python使用正则表达式检测给定的URL地址是否合法
python使用正则表达式检测给定的URL地址是否合法 # python使用正则表达式检测给定的URL地址是否合法 # python使用正则表达式检测给定的URL地址是否合法 # Check if a ...
- python使用正则表达式验证邮箱地址语法有效性
python使用正则表达式验证邮箱地址语法有效性 #python使用正则表达式验证邮箱地址语法有效性 import re # mail regular expression formula# rege ...
最新文章
- [BZOJ 1014][JSOI2008]火星人prefix(Splay+二分+hash)
- 今天抽点时间来说一个C#里的关键字及它们的原型
- 开放下载!《15分钟打造你自己的小程序》(内附详细代码)
- Linux下的字符处理命令之tr命令详解
- TypeError: only integer scalar arrays can be converted to a scalar index
- 没有5G也很香!iPhone去年四季度出货量了解下
- mysql int 判断_PHP通过PDO查MySQL查询int字段返回string类型,解决方案
- mysql BDB支持表锁吗_mysql 表锁问题
- const VS readonly
- 苹果录制屏幕在哪设置_屏幕录像专家如何录全屏 屏幕录像专家全屏录制设置方法...
- 一招解决windows电脑禁用笔记本自带键盘问题
- 照片调色系列教程(一):打造格调美女
- Python创建文件夹和子文件夹
- Halide学习笔记----Halide tutorial源码阅读5
- Win11快捷键切换输入法无反应怎么办?快捷键切换输入法没有反应
- python/sympy计算施密特正交化向量
- php startwith endwith
- 健身气功----八段锦
- win10+python3.6+百度AI——实现人脸识别
- 软件开发技术,自学靠谱吗?
热门文章
- IDEA自动生成类注解,IDEA作者信息自动生成,IDEA类信息自动生成
- GitHub 中 Merge pull request 的 3 中选项说明
- java mybatis基础
- 【Sql Server】DateBase-自动化
- 【Design pattern】设计模式思路总结(二)
- Visual Studio 中文显示乱码问题
- 【Tensorflow】tf.nn.atrous_conv2d如何实现空洞卷积?膨胀卷积
- viterbi维特比算法和隐马尔可夫模型(HMM)
- 基于TensorRT优化的Machine Translation
- 基于ARM Cortex-M的SoC存储体系结构和实战