python正则表达式re.match()匹配多个字符方法的实现


Posted in Python onJanuary 27, 2021

1.  *表示匹配任意多个字符   \d*表示匹配任意多个数字字符

import re
 
text = "123h1ello world"
text1 = "123Hello world456"
text2 = "hello world"
 
res = re.match("\d*", text)  
res1 = re.match("\d*", text1)
res2 = re.match("\d*", text2)
 
print(res.group())
print(res1.group())
print(res2.group())

输出结果为

123
123

Process finished with exit code 0

示例2:*

需求:匹配出,一个字符串第一个字母为大小字符,后面都是小写字母并且这些小写字母可有可无

import re
#注意是前一个字符
ret = re.match("[A-Z][a-z]*","M")
print(ret.group())

ret = re.match("[A-Z][a-z]*","AaBcDE")
print(ret.group())

ret = re.match("[A-Z][a-z]*","Aabcdef")
print(ret.group())

#运行结果
M
Aa
Aabcdef

2.  +表示匹配1个或者多个任意字符   \w+表示匹配一个或多个字母,数字或下划线

import re
 
text = "he+llo world"
text1 = "Hello world456"
text2 = "+hello world"
 
res = re.match("\w+", text)
res1 = re.match("\w+", text1)
res2 = re.match("\w+", text2)
 
print(res.group())
print(res1.group())
print(res2)

输出结果为

he
Hello
None

Process finished with exit code 0

示例2:+

需求:匹配出,变量名是否有效

import re
names = ["name1","_name","2_name","__name__"]
for i in names:
  ret = re.match("[a-zA-Z_]+[\w]*",i)
  if ret:
    print("变量名 %s 符合要求" % ret.group())
  else:
    print("变量名 %s 非法" % i)

#运行结果
变量名 name1 符合要求
变量名 _name 符合要求
变量名 2_name 非法
变量名 __name__ 符合要求

3.  ?表示匹配0个或一个字符     \w?表示匹配0或1个字母,数字或下划线

import re
 
text = "he+llo world"
text1 = "Hello world456"
text2 = "+hello world"
 
res = re.match("\w?", text)
res1 = re.match("\w?", text1)
res2 = re.match("\w?", text2)
 
print(res.group())
print(res1.group())
print(res2.group())

输出结果为

h
H

Process finished with exit code 0

示例2:?

需求:匹配出0到99之间的数字

import re

ret = re.match("[1-9]?[0-9]","7")
print(ret.group())

ret = re.match("[1-9]?\d","33")
print(ret.group())

ret = re.match("[1-9]?\d","09")
print(ret.group())

#运行结果
7
33
0 # 这个结果并不是想要的,利用$才能解决

4.  {m}表示匹配m个字符    \d{11}表示匹配11个数字字符

import re
 
text = "he+llo world"
text1 = "Hello world456"
text2 = "hello world"
 
res = re.match("\w{2}", text)
res1 = re.match("\w{3}", text1)
res2 = re.match("\w{4}", text2)
 
print(res.group())
print(res1.group())
print(res2.group())

输出结果为

he
Hel
hell

Process finished with exit code 0

示例2:{m}
需求:匹配出,8到20位的密码,可以是大小写英文字母、数字、下划线

import re
ret = re.match("[a-zA-Z0-9_]{6}","123a3g45678")
print(ret.group())

ret = re.match("[[a-zA-Z0-9_]{8,20}","1ad3123456addfcasdef")
print(ret.group())

#运行结果
123a3g
1ad3123456addfcasdef

5.   {m,n}表示匹配m-n个字符   \w{2,4}表示匹配2-4个字符

import re
 
text = "hello world"
text1 = "Helloworld456"
text2 = "hello world"
 
res = re.match("\w{2,5}", text)
res1 = re.match("\w{6,8}", text1)
res2 = re.match("\w{20,25}", text2)
 
print(res.group())
print(res1.group())
print(res2)

hello
Hellowor
None

Process finished with exit code 0

到此这篇关于python正则表达式re.match()匹配多个字符方法的实现的文章就介绍到这了,更多相关python re.match()匹配字符内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章希望大家以后多多支持三水点靠木!

Python 相关文章推荐
python调用windows api锁定计算机示例
Apr 17 Python
python实现的登录和操作开心网脚本分享
Jul 09 Python
Python利用多进程将大量数据放入有限内存的教程
Apr 01 Python
python实现冒泡排序算法的两种方法
Mar 10 Python
Python处理菜单消息操作示例【基于win32ui模块】
May 09 Python
Python读取excel中的图片完美解决方法
Jul 27 Python
Python实现DDos攻击实例详解
Feb 02 Python
Python中的支持向量机SVM的使用(附实例代码)
Jun 26 Python
win10安装python3.6的常见问题
Jul 01 Python
基于python判断字符串括号是否闭合{}[]()
Sep 21 Python
Python中相见恨晚的技巧
Apr 13 Python
jupyter notebook保存文件默认路径更改方法汇总(亲测可以)
Jun 09 Python
python工具快速为音视频自动生成字幕(使用说明)
Jan 27 #Python
详解appium自动化测试工具(monitor、uiautomatorviewer)
Jan 27 #Python
Python利用socket模块开发简单的端口扫描工具的实现
Jan 27 #Python
Python爬虫实现selenium处理iframe作用域问题
Jan 27 #Python
python利用appium实现手机APP自动化的示例
Jan 26 #Python
python 基于opencv去除图片阴影
Jan 26 #Python
python中用ggplot绘制画图实例讲解
Jan 26 #Python
You might like
ajax取消挂起请求的处理方法
2013/03/18 PHP
php获取数组中重复数据的两种方法
2013/06/28 PHP
php缓冲 output_buffering和ob_start使用介绍
2014/01/30 PHP
简单谈谈PHP中的Reload操作
2016/12/12 PHP
Laravel框架控制器的request与response用法示例
2019/09/30 PHP
浅谈PHP5.6 与 PHP7.0 区别
2019/10/09 PHP
用JS操作FRAME中的IFRAME及其内容的实现代码
2008/07/26 Javascript
解析js中获得父窗口链接getParent方法以及各种打开窗口的方法
2013/06/19 Javascript
jqTransform美化表单
2015/10/10 Javascript
基于JS实现EOS隐藏错误提示层代码
2016/04/25 Javascript
非常漂亮的相册集 使用jquery制作相册集
2016/04/28 Javascript
JS弹出窗口的运用与技巧大全
2016/11/01 Javascript
JavaScript 动态三角函数实例详解
2017/01/08 Javascript
完美实现js焦点轮播效果(一)
2017/03/07 Javascript
Angular 4依赖注入学习教程之组件服务注入(二)
2017/06/04 Javascript
灵活使用console让js调试更简单的方法步骤
2019/04/23 Javascript
解决layui页面按钮点击无反应,也不报错的问题
2019/09/29 Javascript
微信小程序分享小程序码的生成(带参数)以及参数的获取
2020/03/25 Javascript
对python sklearn one-hot编码详解
2018/07/10 Python
利用django-suit模板添加自定义的菜单、页面及设置访问权限
2018/07/13 Python
浅谈python中拼接路径os.path.join斜杠的问题
2018/10/23 Python
python实现文件的分割与合并
2019/08/29 Python
Python + Flask 实现简单的验证码系统
2019/10/01 Python
如何基于python实现年会抽奖工具
2020/10/20 Python
Ubuntu 20.04安装Pycharm2020.2及锁定到任务栏的问题(小白级操作)
2020/10/29 Python
英国绿色商店:Natural Collection
2019/05/03 全球购物
Fanatics官网:运动服装、球衣、运动装备
2020/10/12 全球购物
乡镇干部先进事迹材料
2014/02/03 职场文书
公司总经理岗位职责
2014/03/15 职场文书
四下基层实施方案
2014/03/28 职场文书
高考励志标语
2014/06/05 职场文书
2014年协会工作总结
2014/11/22 职场文书
写给孩子的新学期寄语
2015/02/27 职场文书
庆祝教师节新闻稿
2015/07/17 职场文书
家属联谊会致辞
2015/07/31 职场文书
使用Pytorch训练two-head网络的操作
2021/05/28 Python