通过正则表达式对字符串进行匹配,使用模块re。
In [8]: # re.match("正则表达式,需要匹配的字符串")
In [13]: re.match(r"nihao", "nihaoa")
Out[13]: <_sre.SRE_Match object; span=(0, 5), match='nihao'>
In [14]: re.match(r"nihao", "n ihaoa")
In [15]: re.match(r"new1", "new")
In [16]: re.match(r"new1", "new1")
Out[16]: <_sre.SRE_Match object; span=(0, 4), match='new1'>
In [17]: re.match(r"new1", "new2")
In [18]: re.match(r"new[1-5]", "new2")
Out[18]: <_sre.SRE_Match object; span=(0, 4), match='new2'>
In [19]: re.match(r"new[1-5]", "new0")
In [20]: re.match(r"new[1-5]", "new1")
Out[20]: <_sre.SRE_Match object; span=(0, 4), match='new1'>
In [21]: re.match(r"new[1-5]", "new3")
Out[21]: <_sre.SRE_Match object; span=(0, 4), match='new3'>
In [22]: re.match(r"new[1-5]", "new5")
Out[22]: <_sre.SRE_Match object; span=(0, 4), match='new5'>
In [23]: re.match(r"new[1-5]", "new6")
In [24]: re.match(r"new[1-5a-b]", "new60")
In [25]: re.match(r"new[1-5a-b]", "new6o")
In [26]: re.match(r"new[1-5a-b]", "new6b")
In [27]: re.match(r"new[1-5a-b]", "new5b")
Out[27]: <_sre.SRE_Match object; span=(0, 4), match='new5'>
In [28]: re.match(r"new[1-5a-b]", "new3a")
Out[28]: <_sre.SRE_Match object; span=(0, 4), match='new3'>
In [29]: re.match(r"new[1-5a-z]", "new3k")
Out[29]: <_sre.SRE_Match object; span=(0, 4), match='new3'>
In [30]: re.match(r"new[1-5a-z]", "new3y")
Out[30]: <_sre.SRE_Match object; span=(0, 4), match='new3'>
# 不同的匹配条件需要用[ ]来隔开。
In [34]: re.match(r"new[1-5][a-z]", "new3yooo")
Out[34]: <_sre.SRE_Match object; span=(0, 5), match='new3y'>
匹配到数据,使用group方法来提取数据
In [32]: re.match(r"new[1-5a-z]", “new3yooo”).group()
Out[32]: ‘new3’
In [33]: re.match(r"new[1-5][a-z]", “new3yooo”).group()
Out[33]: ‘new3y’
匹配单个字符
字符 功能
. 匹配任意1个字符(除了\n)
[ ] 匹配[ ]中列举的字符
\d 匹配数字,即0-9
\D 匹配非数字,即不是数字
\s 匹配空白,即 空格,tab键
\S 匹配非空白
\w 匹配单词字符,即a-z、A-Z、0-9、_
\W 匹配非单词字符
. 匹配任意1个字符(除了\n)
In [92]: re.match(r".", "1").group()
Out[92]: '1'
In [93]: re.match(r".", "a").group()
Out[93]: 'a'
In [95]: re.match(r".", "]").group()
Out[95]: ']'
In [96]: re.match(r".", "!").group()
Out[96]: '!'
[ ] 匹配[ ]中列举的字符
In [98]: re.match(r"a[a-z]", "aa").group()
Out[98]: 'aa'
In [99]: re.match(r"a[a-z]", "ab").group()
Out[99]: 'ab'
In [100]: re.match(r"a[a-z]", "ay").group()
Out[100]: 'ay'
In [101]: re.match(r"a[a-z]", "ae").group()
Out[101]: 'ae'
In [102]: re.match(r"a[a-z][1-9]", "ae2").group()
Out[102]: 'ae2'
In [103]: re.match(r"a[a-z][1-9]", "ae7").group()
Out[103]: 'ae7'
In [104]: re.match(r"a[a-z][1-9]", "aw7").group()
Out[104]: 'aw7'
\d 匹配数字,即0-9
In [105]: re.match(r"a\d", "a7").group()
Out[105]: 'a7'
In [106]: re.match(r"a\d", "a0").group()
Out[106]: 'a0'
In [107]: re.match(r"a\d", "a3").group()
Out[107]: 'a3'
In [108]: re.match(r"a\d", "aa").group()
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-108-50b534a414ea> in <module>
----> 1 re.match(r"a\d", "aa").group()
AttributeError: 'NoneType' object has no attribute 'group'
\D 匹配非数字,即不是数字
In [111]: re.match(r"a\D", "aa").group()
Out[111]: 'aa'
In [112]: re.match(r"a\D", "a!").group()
Out[112]: 'a!'
In [113]: re.match(r"a\D", "a>").group()
Out[113]: 'a>'
In [114]: re.match(r"a\D", "a3").group()
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-114-bf1eac292df0> in <module>
----> 1 re.match(r"a\D", "a3").group()
AttributeError: 'NoneType' object has no attribute 'group'
\s 匹配空白,即 空格,tab键
\S 匹配非空白
n [118]: re.match(r"a\s", "a ").group()
Out[118]: 'a '
In [119]: re.match(r"a\S", "a ").group()
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-119-5860cfd22cd4> in <module>
----> 1 re.match(r"a\S", "a ").group()
AttributeError: 'NoneType' object has no attribute 'group'
In [120]: re.match(r"a\S", "a1").group()
Out[120]: 'a1'
In [121]: re.match(r"a\S", "aa").group()
Out[121]: 'aa'
In [122]: re.match(r"a\S", "a?").group()
Out[122]: 'a?'
\w 匹配单词字符,即a-z、A-Z、0-9、_
\W 匹配非单词字符
In [124]: re.match(r"a\w", "as").group()
Out[124]: 'as'
In [125]: re.match(r"a\w", "a2").group()
Out[125]: 'a2'
In [126]: re.match(r"a\w", "a?").group()
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-126-f6b67b861ef9> in <module>
----> 1 re.match(r"a\w", "a?").group()
AttributeError: 'NoneType' object has no attribute 'group'
In [127]: re.match(r"a\w", "aA").group()
Out[127]: 'aA'
In [128]: re.match(r"a\W", "aA").group()
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-128-0fb6476028ab> in <module>
----> 1 re.match(r"a\W", "aA").group()
AttributeError: 'NoneType' object has no attribute 'group'
In [129]: re.match(r"a\W", "a?").group()
Out[129]: 'a?'
In [130]: re.match(r"a\W", "a.").group()
Out[130]: 'a.'
匹配多个字符
字符 功能
*匹配前一个字符出现0次或者无限次,即可有可无
+匹配前一个字符出现1次或者无限次,即至少有1次
? 匹配前一个字符出现1次或者0次,即要么有1次,要么没有
{m} 匹配前一个字符出现m次
{m,n} 匹配前一个字符出现从m到n次
正则表达式{ }的作用
{m} 匹配前一个字符出现m次
{m,n} 匹配前一个字符出现从m到n次
In [57]: # 正则表达式{}的作用
In [58]: # 匹配前一个字符出现的次数
In [59]: re.match("雪中悍刀行", "雪中悍刀行").group()
Out[59]: '雪中悍刀行'n
[62]: re.match("雪中悍刀行A{2}", "雪中悍刀行AAA").group()
Out[62]: '雪中悍刀行AA'
In [63]: re.match("雪中悍刀行A{2}", "雪中悍刀行AAAA").group()
Out[63]: '雪中悍刀行AA'
In [64]: re.match("雪中悍刀行A{2,4}", "雪中悍刀行AAAA").group()
Out[64]: '雪中悍刀行AAAA'
In [65]: re.match("雪中悍刀行A{2,4}", "雪中悍刀行AAA").group()
Out[65]: '雪中悍刀行AAA'
In [66]: re.match("雪中悍刀行1{2,4}", "雪中悍刀行11").group()
Out[66]: '雪中悍刀行11'
In [67]: re.match("雪中悍刀行1{2,4}", "雪中悍刀行111").group()
Out[67]: '雪中悍刀行111'
In [68]: re.match("雪中悍刀行1{2,4}", "雪中悍刀行1111").group()
Out[68]: '雪中悍刀行1111'
In [69]: re.match("雪中悍刀行\d{2,4}", "雪中悍刀行1111").group()
Out[69]: '雪中悍刀行1111'
In [70]: re.match("雪中悍刀行\d{2,4}", "雪中悍刀行22").group()
Out[70]: '雪中悍刀行22'
In [71]: re.match("雪中悍刀行\d{2,4}", "雪中悍刀行444").group()
Out[71]: '雪中悍刀行444'
In [72]: re.match("雪中悍刀行\d{2,4}", "雪中悍刀行4445").group()
Out[72]: '雪中悍刀行4445'
In [73]: re.match("雪中悍刀行\d{2,4}", "雪中悍刀行1234").group()
Out[73]: '雪中悍刀行1234'
In [74]: re.match("雪中悍刀行\d{2,4}", "雪中悍刀行34").group()
Out[74]: '雪中悍刀行34'
In [75]: re.match("雪中悍刀行\d{2,4}", "雪中悍刀行234").group()
Out[75]: '雪中悍刀行234'
? 匹配前一个字符出现1次或者0次,即要么有1次,要么没有
例如:匹配手机号
In [79]: re.match("010-\d{8}", "010-12345678").group()
Out[79]: '010-12345678'
In [80]: re.match(r"010-\d{8}", "010-12345678").group()
Out[80]: '010-12345678'
In [81]: re.match(r"\d{3}-\d{8}", "010-12345678").group()
Out[81]: '010-12345678'
In [82]: re.match(r"\d{3,4}-\d{8}", "0635-12345678").group()
Out[82]: '0635-12345678'
In [83]: re.match(r"\d{3,4}-?\d{8}", "0635-12345678").group()
Out[83]: '0635-12345678'
In [84]: re.match(r"\d{3,4}-?\d{8}", "063512345678").group()
Out[84]: '063512345678'
In [85]: re.match(r"\d{3,4}-?\d{7,8}", "0635-1234567").group()
Out[85]: '0635-1234567'
In [86]: re.match(r"\d{3,4}-?\d{7,8}", "0635-12345678").group()
Out[86]: '0635-12345678
*匹配前一个字符出现0次或者无限次,即可有可无
+匹配前一个字符出现1次或者无限次,即至少有1次
In [87]: re.match(".*", "").group()
Out[87]: ''
In [88]: re.match(".*", " ").group()
Out[88]: ' '
In [89]: re.match(".+", " ").group()
Out[89]: ' '
In [90]: re.match(".+", "").group()
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-90-2135ae78ca01> in <module>
----> 1 re.match(".+", "").group()
AttributeError: 'NoneType' object has no attribute 'group'
- .+ 不能匹配空字符
需求:匹配出,变量名是否有效
import re
In [136]: names = ['name1', '_name','2_name','__name__']
In [137]: names
Out[137]: ['name1', '_name', '2_name', '__name__']
In [138]: for name in names:
...: s = re.match("\D[a-zA-Z_]*", name)
...: if s:
...: print("变量名%s匹配成功"%s.group())
...: else:
...: print("%s匹配失败"%name)
...:
变量名name匹配成功
变量名_name匹配成功
2_name匹配失败
变量名__name__匹配成功
匹配开头结尾
字符 功能
^ 匹配字符串开头
$ 匹配字符串结尾
没有 ^ 和 $
# 代码
import re
def main():
names = ['age1', '1age', '_age8', 'age#7', 'age!', 'age_2_', 'a_age', 'age*', 'age+','______']
for name in names:
ret = re.match(r"[a_zA-Z_][0-9A-Za-z_]*", name)
if ret:
print("匹配结果%s,正则匹配的结果为:%s"% name)
else:
print("匹配不成功%s" % name)
if __name__ == "__main__":
main()
运行结果:
匹配结果age1
匹配不成功1age
匹配结果_age8
匹配结果age#7
匹配结果age!
匹配结果age_2_
匹配结果a_age
匹配结果age*
匹配结果age+
匹配不成功-----
加上 ^ 和 $
import re
def main():
names = ['age1', '1age', '_age8', 'age#7', 'age!', 'age_2_', 'a_age', 'age*', 'age+','______']
for name in names:
ret = re.match(r"^[a_zA-Z_][0-9A-Za-z_]*$", name)
if ret:
print("匹配结果%s,正则匹配的结果为:%s"%( name,ret.group()))
else:
print("匹配不成功%s" % name)
if __name__ == "__main__":
main()
运行结果:
bogon:Desktop zou$ python3 00正则匹配变量名.py
匹配结果age1,正则匹配的结果为:age1
匹配不成功1age
匹配结果_age8,正则匹配的结果为:_age8
匹配不成功age#7
匹配不成功age!
匹配结果age_2_,正则匹配的结果为:age_2_
匹配结果a_age,正则匹配的结果为:a_age
匹配不成功age*
匹配不成功age+
匹配结果______,正则匹配的结果为:______