当前位置: 首页 > 图灵资讯 > 行业资讯> Python正则表达式字符串的组成

Python正则表达式字符串的组成

来源:图灵python
时间: 2024-06-24 13:47:30

说到正则表达式,它将经常被学习一段时间的朋友使用。在本文中,我们需要了解正则表达式字符串的组成部分。

1、正则表达式字符串由普通字符和元字符组成。

2、普通字符是根据字符的字面意义表示的字符。元字符是预先定义的一些特定字符。

实例

importre

#字符串1
regx_string='aab'

#字符串2
regx_string2='anb'

#生成匹配的正则表达对象
pattern=re.compile('a.b')

#匹配字符串1
m1=pattern.match(regx_string)

print(m1)
#<_sre.SRE_Matchobject;span=(0,3),match='aab'>

#匹配字符串2
m2=pattern.match(regx_string2)

print(m2)
#<_sre.SRE_Matchobject;span=(0,3),match='anb'>

#字符串3
regx_string3='and'

m3=pattern.match(regx_string3)

print(m3)
#None

以上是Python正则表达式字符串的组成,希望对大家有所帮助。更多Python学习指导:python基础教程

本教程的操作环境:windows7系统,Python 3.9.1,DELL G3电脑。