python正则表达式re.match()匹配多个字符方法的实现
作者:zjLOVEcyj 发布时间:2023-07-30 08:25:16
标签:python,re.match()
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
123Process 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
NoneProcess 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
HProcess 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
hellProcess 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
NoneProcess finished with exit code 0
来源:https://blog.csdn.net/cyj5201314/article/details/104456005


猜你喜欢
- JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式,采用完全独立于语言的文本格式,是理想的数据交换
- forma格式化的用法format函数可以接受不限个参数,位置可以不按顺序。基本语法是通过{ }和:来代替c语言的%。>>>
- 在今天的设计中,排版常常被忽视,特别是被网页设计师忽视。这真是件遗憾的事情因为CSS可以做很多事情来控制我们的排版。也就是说,我们被局限于某
- 在 Python 中对一个可迭代对象进行排序是很常见的一个操作,一般会用到 sorted() 函数num_list = [4, 2, 8,
- 本文实例为大家分享了python多进程读图提取特征存npy的具体代码,供大家参考,具体内容如下import multiprocessingi
- SQL语句中要不要加单引号?犯了一个超级超级蠢的错误,把population写成polulation而不自知,多次提醒我找不到polulat
- 再看继承真正的大餐来之前,还是来点儿开胃菜!回顾一下关于类的继承的知识:我们先看上面的代码,这是一个简单的类继承,我们可以看到父类Base和
- 1、pyinstaller的使用网上资料多,此处省略2、打包时报错1、可能有些包没有安装(跑跑程序不缺库就行)2、有些包pyinstalle
- 前言在了解了REST farmwork封装的视图类之后,我对python的面向对象有了更深刻的理解。Django RESR framewor
- 在许多网页中,当鼠标移到一张图片上时,又弹出另一张图片,做这种广告条,要用到Macromedia DreamWeaver中的Lay
- 文章是从stackoverflow翻译过来的,原文地址:Relative imports for the billionth time本文要
- <%Class Cls_TemplateDim RegDim PageDim CIDDim SIDDim RuleDim Conten
- 这是一个小技巧,看着.ejs的后缀总觉得不爽,使用如下方法,可以将模板文件的后缀换成我们习惯的.html。1.在app.js的头上定义ejs
- 有时候我们只知道列的名字,但是不知道这列数据到底在哪个表里面,那么可以用下面的办法把含有这列数据的表查找出来。Select O.name o
- pycharm没找到database问题pycharm的社区版本身是没有database的;只有专业版里面会直接出现右上角的database
- map()是python的一个内建函数, 他能够通过函数来处理序列,比如,我们相关一个数组[0,1,2,3,4,5]所有的数字都+2 , 当
- 问题描述因为项目强制关闭,但是服务还在运行,导致重新运行项目时候 提示地址已经使用(端口被占用)/usr/bin/python3.5 pyt
- 前言某个夜深人静的夜晚,夜微凉风微扬,月光照进我的书房~当我打开文件夹以回顾往事之余,惊现许多看似杂乱的无聊代码。我拍腿正坐,一个想法油然而
- Git修改已提交的commit注释两种情况:修改最后一次注释1、在命令行输入如下命令,然后回车:git commit --amend2、在命
- 本文实例讲述了Python基于TCP实现会聊天的小机器人功能。分享给大家供大家参考,具体如下:一 代码1、服务端程序import socke