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的Django框架的运行方式及处理流程
Apr 08 Python
状态机的概念和在Python下使用状态机的教程
Apr 11 Python
Python爬虫实现百度图片自动下载
Feb 04 Python
Python发送http请求解析返回json的实例
Mar 26 Python
python3中os.path模块下常用的用法总结【推荐】
Sep 16 Python
python  创建一个保留重复值的列表的补码
Oct 15 Python
pycharm 解除默认unittest模式的方法
Nov 30 Python
Python用61行代码实现图片像素化的示例代码
Dec 10 Python
Python爬虫程序架构和运行流程原理解析
Mar 09 Python
python3 sorted 如何实现自定义排序标准
Mar 12 Python
解决python Jupyter不能导入外部包问题
Apr 15 Python
在pycharm中无法import所安装的库解决方案
May 31 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格式化金额函数分享
2015/02/02 PHP
PHP模板引擎Smarty中变量的使用方法示例
2016/04/11 PHP
基于jquery的复制网页内容到WORD的实现代码
2011/02/16 Javascript
js编写trim()函数及正则表达式的运用
2013/10/24 Javascript
jQuery中的val()示例应用
2014/02/26 Javascript
完美兼容各大浏览器获取HTTP_REFERER方法总结
2014/06/24 Javascript
jQuery入门介绍之基础知识
2015/01/13 Javascript
浅谈JavaScript中的字符编码转换问题
2015/07/07 Javascript
jQuery入门基础知识学习指南
2015/08/14 Javascript
简单封装js的dom查询实例代码
2016/07/08 Javascript
bootstrap表格分页实例讲解
2016/12/30 Javascript
jQuery加载及解析XML文件的方法实例分析
2017/01/22 Javascript
JS常用正则表达式总结【经典】
2017/05/12 Javascript
jQuery实现切换隐藏与显示同时切换图标功能
2017/10/29 jQuery
Router解决跨模块下的页面跳转示例
2018/01/11 Javascript
bootstrap中日历范围选择插件daterangepicker的使用详解
2018/04/17 Javascript
新手快速上手webpack4打包工具的使用详解
2019/01/28 Javascript
LayUi数据表格自定义赋值方式
2019/10/26 Javascript
[01:55]2014DOTA2国际邀请赛快报:国土生病 紧急去医院治疗
2014/07/10 DOTA
OpenCV搞定腾讯滑块验证码的实现代码
2019/05/18 Python
Django项目后台不挂断运行的方法
2019/08/31 Python
Python3 把一个列表按指定数目分成多个列表的方式
2019/12/25 Python
基于python requests selenium爬取excel vba过程解析
2020/08/12 Python
css3+jq创作含苞待放的荷花
2014/02/20 HTML / CSS
Solaris操作系统的线程机制
2012/12/23 面试题
自荐信范文
2013/12/10 职场文书
销售员求职个人的自我评价
2014/02/19 职场文书
分公司任命书
2014/06/06 职场文书
小学班主任事迹材料
2014/12/17 职场文书
先进单位申报材料
2014/12/25 职场文书
承兑汇票延期证明
2015/06/23 职场文书
远程教育培训心得体会
2016/01/09 职场文书
交通事故协议书范本
2016/03/19 职场文书
python scipy 稀疏矩阵的使用说明
2021/05/26 Python
Python爬虫基础初探selenium
2021/05/31 Python
python单向链表实例详解
2022/05/25 Python