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中实现结构相似的函数调用方法
Mar 10 Python
用实例解释Python中的继承和多态的概念
Apr 27 Python
python开发之函数定义实例分析
Nov 12 Python
python中os模块详解
Oct 14 Python
对Python中range()函数和list的比较
Apr 19 Python
Python学习_几种存取xls/xlsx文件的方法总结
May 03 Python
Python字典中的键映射多个值的方法(列表或者集合)
Oct 17 Python
利用python实现在微信群刷屏的方法
Feb 21 Python
Python实现连接MySql数据库及增删改查操作详解
Apr 16 Python
python 表格打印代码实例解析
Oct 12 Python
Python 如何操作 SQLite 数据库
Aug 17 Python
Python基础之函数嵌套知识总结
May 23 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
php删除数组元素示例分享
2014/02/17 PHP
Linux下安装PHP MSSQL扩展教程
2014/10/24 PHP
Laravel 实现密码重置功能
2018/02/23 PHP
PHP使用 Imagick 扩展实现图片合成,圆角处理功能示例
2019/09/09 PHP
关于使用runtimeStyle属性问题讨论文章
2007/03/08 Javascript
Display SQL Server Version Information
2007/06/21 Javascript
ASP SQL防注入的方法
2008/12/25 Javascript
基于jquery的一个OutlookBar类,动态创建导航条
2010/11/19 Javascript
SeaJS入门教程系列之完整示例(三)
2014/03/03 Javascript
DOM节点的替换或修改函数replaceChild()用法实例
2015/01/12 Javascript
基于jquery实现在线选座订座之影院篇
2015/08/24 Javascript
微信JSAPI支付操作需要注意的细节
2017/01/10 Javascript
jQuery实现用户输入自动完成功能
2017/02/13 Javascript
微信小程序+腾讯地图开发实现路径规划绘制
2019/05/22 Javascript
vue history 模式打包部署在域名的二级目录的配置指南
2019/07/02 Javascript
详细教你微信公众号正文页SVG交互开发技巧
2019/07/25 Javascript
node读写Excel操作实例分析
2019/11/06 Javascript
详解webpack-dev-middleware 源码解读
2020/03/23 Javascript
手写Vue源码之数据劫持示例详解
2021/01/04 Vue.js
[15:41]教你分分钟做大人——灰烬之灵
2015/03/11 DOTA
Python中的pass语句使用方法讲解
2015/05/14 Python
Python使用openpyxl读写excel文件的方法
2017/06/30 Python
python中 logging的使用详解
2017/10/25 Python
对python 读取线的shp文件实例详解
2018/12/22 Python
Django自定义用户登录认证示例代码
2019/06/30 Python
python中将两组数据放在一起按照某一固定顺序shuffle的实例
2019/07/15 Python
python实现按首字母分类查找功能
2019/10/31 Python
py-charm延长试用期限实例
2019/12/22 Python
Django choices下拉列表绑定实例
2020/03/13 Python
Python Pandas list列表数据列拆分成多行的方法实现
2020/12/14 Python
仓库主管的岗位职责
2013/12/04 职场文书
项目资料员岗位职责
2013/12/10 职场文书
采购人员的个人自我评价
2014/01/16 职场文书
教师批评与自我批评发言稿
2014/10/15 职场文书
工程部经理岗位职责
2015/02/02 职场文书
python随机打印成绩排名表
2021/06/23 Python