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

 更新时间:2021年01月27日 09:57:36   作者:zjLOVEcyj  
这篇文章主要介绍了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
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多线程semaphore实现线程数控制的示例

    python多线程semaphore实现线程数控制的示例

    这篇文章主要介绍了python多线程semaphore实现线程数控制的示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-08-08
  • Python对excel的基本操作方法

    Python对excel的基本操作方法

    这篇文章主要介绍了Python对excel的基本操作,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-02-02
  • Python基于numpy模块实现回归预测

    Python基于numpy模块实现回归预测

    这篇文章主要介绍了Python基于numpy模块实现回归预测,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-05-05
  • Python中with及contextlib的用法详解

    Python中with及contextlib的用法详解

    这篇文章主要介绍了Python中with及contextlib的用法,结合实例形式较为详细的分析了with及contextlib的功能、使用方法与相关注意事项,需要的朋友可以参考下
    2017-06-06
  • 使用Python3编写抓取网页和只抓网页图片的脚本

    使用Python3编写抓取网页和只抓网页图片的脚本

    这篇文章主要介绍了使用Python3编写抓取网页和只抓网页图片的脚本,使用到了urllib模块,需要的朋友可以参考下
    2015-08-08
  • opencv中cv2.minAreaRect函数输出角度问题详解

    opencv中cv2.minAreaRect函数输出角度问题详解

    minAreaRect返回的数据包括了矩形的中心点,宽、高,和旋转角度,下面这篇文章主要给大家介绍了关于opencv中cv2.minAreaRect函数输出角度问题的相关资料,需要的朋友可以参考下
    2022-11-11
  • python简单的函数定义和用法实例

    python简单的函数定义和用法实例

    这篇文章主要介绍了python简单的函数定义和用法,实例分析了Python自定义函数及其使用方法,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-05-05
  • python 读写csv文件方式(创建,追加,覆盖)

    python 读写csv文件方式(创建,追加,覆盖)

    这篇文章主要介绍了python 读写csv文件方式(创建,追加,覆盖),具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-05-05
  • Python大数据量文本文件高效解析方案代码实现全过程

    Python大数据量文本文件高效解析方案代码实现全过程

    在数据分析中,有时数据源会是超大的文本文件(几G,或在几十G),需要从中提取需要的信息,下面这篇文章主要给大家介绍了关于Python大数据量文本文件高效解析方案代码实现的相关资料,需要的朋友可以参考下
    2022-12-12
  • Python 制作词云的WordCloud参数用法说明

    Python 制作词云的WordCloud参数用法说明

    这篇文章主要介绍了Python 制作词云的WordCloud参数用法说明,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2021-03-03

最新评论