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标准库06之子进程 (subprocess包) 详解
Dec 07 Python
利用Python脚本生成sitemap.xml的实现方法
Jan 31 Python
Python中列表list以及list与数组array的相互转换实现方法
Sep 22 Python
Python使用pymysql从MySQL数据库中读出数据的方法
Jul 25 Python
计算机二级python学习教程(2) python语言基本语法元素
May 16 Python
Python 类方法和实例方法(@classmethod),静态方法(@staticmethod)原理与用法分析
Sep 20 Python
python实现的Iou与Giou代码
Jan 18 Python
tensorflow 自定义损失函数示例代码
Feb 05 Python
python设置环境变量的作用整理
Feb 17 Python
如何真正的了解python装饰器
Aug 14 Python
Python3+PyCharm+Django+Django REST framework配置与简单开发教程
Feb 16 Python
python lambda的使用详解
Feb 26 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
Mysql的GROUP_CONCAT()函数使用方法
2008/03/28 PHP
PHP 极验验证码实例讲解
2016/09/29 PHP
js获得地址栏?问号后参数的方法
2013/08/08 Javascript
js综合应用实例简单的表格统计
2013/09/03 Javascript
深入分析下javascript中的[]()+!
2015/07/07 Javascript
JS实现带有抽屉效果的产品类网站多级导航菜单代码
2015/09/15 Javascript
canvas实现图片根据滑块放大缩小效果
2017/02/24 Javascript
基于vue.js轮播组件vue-awesome-swiper实现轮播图
2017/03/17 Javascript
微信小程序中setInterval的使用方法
2017/09/29 Javascript
JS 使用 window对象的print方法实现分页打印功能
2018/05/16 Javascript
JS添加或删除HTML dom元素的方法实例分析
2019/03/05 Javascript
webpack打包html里面img后src为“[object Module]”问题
2019/12/22 Javascript
js实现聊天对话框
2020/02/08 Javascript
[00:10]DOTA2全国高校联赛 以DOTA2会友
2018/05/30 DOTA
使用scrapy实现爬网站例子和实现网络爬虫(蜘蛛)的步骤
2014/01/23 Python
Python内置的HTTP协议服务器SimpleHTTPServer使用指南
2016/03/30 Python
Bottle框架中的装饰器类和描述符应用详解
2017/10/28 Python
Python使用matplotlib实现绘制自定义图形功能示例
2018/01/18 Python
python操作mysql代码总结
2018/06/01 Python
django实现将后台model对象转换成json对象并传递给前端jquery
2020/03/16 Python
mac 上配置Pycharm连接远程服务器并实现使用远程服务器Python解释器的方法
2020/03/19 Python
TensorFlow2.X结合OpenCV 实现手势识别功能
2020/04/08 Python
Python网络爬虫四大选择器用法原理总结
2020/06/01 Python
Python爬虫实例——scrapy框架爬取拉勾网招聘信息
2020/07/14 Python
Python字典fromkeys()方法使用代码实例
2020/07/20 Python
css3实现冲击波效果的示例代码
2018/01/11 HTML / CSS
Zalando Lounge瑞士:时尚与生活方式购物俱乐部
2020/03/12 全球购物
拉飞逸官网:Lafayette 148 New York
2020/07/15 全球购物
机关门卫的岗位职责
2014/04/29 职场文书
党员评议个人总结
2014/10/20 职场文书
幼儿园教师考核评语
2014/12/31 职场文书
专家推荐信怎么写
2015/03/25 职场文书
python自动计算图像数据集的RGB均值
2021/06/18 Python
Logback 使用TurboFilter实现日志级别等内容的动态修改操作
2021/08/30 Java/Android
船舶调度指挥系统——助力智慧海事
2022/02/18 无线电
nginx实现多geoserver服务的负载均衡
2022/05/15 Servers