python正则表达式re.match()匹配多个字符方法的实现

作者:zjLOVEcyj 时间:2023-07-30 08:25:16 

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

来源:https://blog.csdn.net/cyj5201314/article/details/104456005

标签:python,re.match()
0
投稿

猜你喜欢

  • Python新手如何理解循环加载模块

    2022-10-09 00:12:53
  • JavaScript每天必学之事件

    2024-04-22 13:08:43
  • pandas学习之txt与sql文件的基本操作指南

    2022-08-03 18:57:50
  • sql server海量数据库的查询优化及分页算法方案

    2010-07-02 21:17:00
  • SQL Server复制功能 巧妙选择避开Bug

    2011-01-04 17:03:00
  • Thinkphp5框架实现获取数据库数据到视图的方法

    2024-05-03 15:52:16
  • Vue实现无限加载瀑布流

    2024-05-09 15:23:16
  • asp如何正确理解MIME类型?

    2010-06-28 18:21:00
  • 详解用selenium来下载小姐姐图片并保存

    2023-08-11 02:02:31
  • python多进程 主进程和子进程间共享和不共享全局变量实例

    2022-11-05 11:42:56
  • vue+elementUI实现动态面包屑

    2024-05-02 17:11:02
  • Python多进程同步Lock、Semaphore、Event实例

    2021-02-22 17:11:26
  • 7个流行的Python强化学习算法及代码实现详解

    2021-07-06 08:38:03
  • Python使用字典的嵌套功能详解

    2021-03-19 01:20:02
  • python数据可视化Pyecharts库sankey修改桑葚图颜色

    2023-09-04 08:56:13
  • 如何用ASP创建日志文件

    2008-03-10 17:27:00
  • python判断一个数是否能被另一个整数整除的实例

    2021-02-06 13:25:42
  • MySQL数据库索引原理及优化策略

    2024-01-20 00:35:42
  • js序列化和反序列化的使用讲解

    2023-08-05 08:13:24
  • go实现文件的创建、删除与读取示例代码

    2023-06-17 05:10:50
  • asp之家 网络编程 m.aspxhome.com