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实现数通设备tftp备份配置文件示例
Apr 02 Python
python k-近邻算法实例分享
Jun 11 Python
Python中请使用isinstance()判断变量类型
Aug 25 Python
python3读取MySQL-Front的MYSQL密码
May 03 Python
Python全局锁中如何合理运用多线程(多进程)
Nov 06 Python
python matplotlib实现将图例放在图外
Apr 17 Python
Python基于Tkinter编写crc校验工具
May 06 Python
Python3.7下安装pyqt5的方法步骤(图文)
May 12 Python
Python Flask框架实现简单加法工具过程解析
Jun 03 Python
Python astype(np.float)函数使用方法解析
Jun 08 Python
scrapy中如何设置应用cookies的方法(3种)
Sep 22 Python
用Python监控你的朋友都在浏览哪些网站?
May 27 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+Html+缓存
2006/11/25 PHP
escape unescape的php下的实现方法
2007/04/27 PHP
PHP扩展编写点滴 技巧收集
2010/03/09 PHP
php判断终端是手机还是电脑访问网站的思路及代码
2013/04/24 PHP
php常见的魔术方法详解
2014/12/25 PHP
jQuery 数据缓存模块进化史详细介绍
2012/11/19 Javascript
js 实现菜单左右滚动显示示例介绍
2013/11/21 Javascript
弹出最简单的模式化遮罩层的js代码
2013/12/04 Javascript
javascript基于DOM实现省市级联下拉框的方法
2015/05/14 Javascript
JavaScript缓冲运动实现方法(2则示例)
2016/01/08 Javascript
NODE.JS跨域问题的完美解决方案
2016/10/20 Javascript
Bootstrap CSS组件之面包屑导航(breadcrumb)
2016/12/17 Javascript
JSON字符串和JSON对象相互转化实例详解
2017/01/05 Javascript
javascript深拷贝和浅拷贝详解
2017/02/14 Javascript
Bootstrap table右键功能实现方法
2017/02/20 Javascript
详解处理bootstrap4不支持远程静态框问题
2018/07/20 Javascript
[01:00:35]2018DOTA2亚洲邀请赛3月30日B组 EffcetVSMineski
2018/03/31 DOTA
python统计日志ip访问数的方法
2015/07/06 Python
Python操作Word批量生成文章的方法
2015/07/28 Python
用tensorflow搭建CNN的方法
2018/03/05 Python
利用pyuic5将ui文件转换为py文件的方法
2019/06/19 Python
使用PYTHON解析Wireshark的PCAP文件方法
2019/07/23 Python
Spring Cloud Feign高级应用实例详解
2019/12/10 Python
推荐8款常用的Python GUI图形界面开发框架
2020/02/23 Python
Django 自定义404 500等错误页面的实现
2020/03/08 Python
Python实现转换图片背景颜色代码
2020/04/30 Python
详解基于Facecognition+Opencv快速搭建人脸识别及跟踪应用
2021/01/21 Python
HTML5 语音搜索(淘宝店语音搜素)
2013/01/03 HTML / CSS
澳大利亚排名第一的露营和户外设备在线零售商:Outbax
2020/05/06 全球购物
构造方法和其他方法的区别?怎么调用父类的构造方法
2013/09/22 面试题
学校门卫管理制度
2014/01/30 职场文书
土建专业毕业生自荐书
2014/07/04 职场文书
工作检讨书500字
2014/10/19 职场文书
2014年学校财务工作总结
2014/12/06 职场文书
事业单位年度考核评语
2014/12/31 职场文书
学生个人总结范文
2015/02/15 职场文书