Python笔记_第四篇_高阶编程_正则表达式_2.正则表达式入门

1. 匹配单个字符和数字

. --->> 匹配除换行符以外的任意字符。
[0123456789] --->> []字符集合,表示匹配方括号中所包含的任意一个字符。
[Thomas] --->> []字符集合,表示匹配方括号中所包含的任意一个字符。匹配'T','h'...任意一个字符。
[a-z] --->> - 匹配任意小写字母
[A-Z] --->> - 匹配任意大写字母
[0-9] --->> 匹配任意数字,类似于[0123456789]
[0-9a-zA-Z] --->> 匹配任意的数字和字母(包含大小写)
[0-9a-zA-Z_] --->> 匹配任意的数字和字母(包含大小写)和下划线
[^Thomas] --->> 匹配除了Thomas这几个字母以外的所有字符,中括号里面的^称为脱字符,表示不匹配集合中的字符。
[^0-9] --->> 匹配所有的非数字字符。
\d --->> 匹配数字,效果同[0-9]
\D --->> 匹配非数字字符,效果同[^0-9]
\w --->> 匹配数字,字母和下划线,效果同[0-9a-zA-Z_],判断标识符比较合适
\W --->> 匹配数字,字母和下划线,效果同[^0-9a-zA-Z_],判断标识符比较合适
\s --->> 匹配任意的空白符(空格,换行,回车,换页,制表),效果同[ \f\n\r\t]
\S --->> 匹配任意的非空白符(空格,换行,回车,换页,制表),效果同[^ \f\n\r\t]

print(re.search(".","Thomas is a good man"))
# <re.Match object; span=(0, 1), match='T'>

print(re.search("[0123456789]","Thomas is a good man_ 7"))
# <re.Match object; span=(21, 22), match='7'>

print(re.search("[Thomas]","Thomas is a good man_ 7"))
# <re.Match object; span=(0, 1), match='T'>

print(re.search("[a-z]","Thomas is a good man_ 7"))
# <re.Match object; span=(1, 2), match='h'>

print(re.search("[A-Z]","Thomas is a good man_ 7"))
# <re.Match object; span=(0, 1), match='T'>

print(re.search("[0-9]","Thomas is a good man_ 7"))
# <re.Match object; span=(22, 23), match='7'>

print(re.search("[0-9a-zA-Z]","Thomas is a good man_ 7"))
# <re.Match object; span=(0, 1), match='T'>

print(re.search("[0-9a-zA-Z_]","_Thomas is a good man_ 7"))
# <re.Match object; span=(0, 1), match='_'>

print(re.search("[^Thomas]","_Thomas is a good man_ 7"))
# <re.Match object; span=(0, 1), match='_'>

print(re.search("[^0-9]","Thomas is a good man_ 7"))
# <re.Match object; span=(0, 1), match='T'>

print(re.search("\d","Thomas is a good man 7"))
# <re.Match object; span=(21, 22), match='7'>

print(re.search("\w","_Thomas is a good man 7"))
# <re.Match object; span=(0, 1), match='_'>

print(re.search("\s","_Thomas is a good man 7"))
# <re.Match object; span=(7, 8), match=' '>

 

2. 锚字符(边界字符)

^ --->> 行首匹配,和在[]里的^不是一个意思
$ --->> 行尾匹配
\A --->> 匹配字符串开始,它和^区别是:\Z只匹配整个字符的开头,即是re.M模式下也不会匹配它行的行首。
\Z --->> 匹配字符串结束,它和$区别是:\Z只匹配整个字符的开头,即是re.M模式下也不会匹配它行的行尾。
\b --->> 匹配单词的边界,也就是值单词和空格间的位置。r'er\b':可以匹配never,不能匹配nerve,单词的边界。
\B --->> 匹配非单词的边界,也就是值单词和空格间的位置。

print(re.search("^Thomas","Thomas is a good man 7"))
# <re.Match object; span=(0, 6), match='Thomas'>

print(re.search("Thomas$","Thomas is a good man Thomas"))
# <re.Match object; span=(21, 27), match='Thomas'>

print(re.findall("^Thomas","Thomas is a good man\nThomas is a nice man",re.M))
# ['Thomas', 'Thomas']

print(re.findall("\AThomas","Thomas is a good man\nThomas is a nice man",re.M))
# ['Thomas']

print(re.findall("man$","Thomas is a good man\nThomas is a nice man",re.M))
# ['man', 'man']

print(re.findall("man\Z","Thomas is a good man\nThomas is a nice man",re.M))
# ['man']

print(re.search(r"er\b","never"))
# <re.Match object; span=(3, 5), match='er'>
print(re.search(r"er\b","nerve"))
# None

print(re.search(r"er\B","never"))
# None
print(re.search(r"er\B","nerve"))
# <re.Match object; span=(1, 3), match='er'>

 

3. 匹配多个字符

说明:下方的x、y、z均为假设的普通字符,n、m是非负整数,不是正则表达式的元字符。
(xyz) --->> 匹配小括号内的xyz(作为一个整体去匹配)
x? --->> 匹配0个或者1个x。
x* --->> 匹配0个或者任意多个个x。(.* 表示匹配0个或者任意多个字符(换行符除外))
x+ --->> 匹配至少一个x
x{n} --->> 匹配确定n个x(n是一个非负整数)
x{n,} --->> 匹配至少n个x
x{n,m} --->> 匹配至少n个最多m个x,注意n<=m。
x|y --->> | 表示或,匹配的是x或y

print(re.findall(r"(Thomas)","Thomasgood is a good man,Thomas is a nice man"))
# ['Thomas', 'Thomas']

print(re.findall(r"o?","ooo")) # 非贪婪匹配(尽可能少的匹配)
# ['o', 'o', 'o', '']

print(re.findall(r"o*","oooboo")) # 贪婪匹配(尽可能多的匹配)
# ['ooo', '', 'oo', '']

print(re.findall(r".*","oooboo"))
# ['oooboo', '']

print(re.findall(r"o+","oooboo"))
# ['ooo', 'oo']

print(re.findall(r"o{2}","oooboo"))
# ['oo', 'oo']

print(re.findall(r"o{2,}","oooboo")) # 也是贪婪匹配
# ['ooo', 'oo']

print(re.findall(r"a{3,6}","aaaabaaa"))
# ['aaaa', 'aaa']  # 最多6个也可以是4个

print(re.findall(r"((t|T)homas)","thomas--Thomas"))
# [('thomas', 't'), ('Thomas', 'T')]  # 这是组

print(re.findall(r"(t|T)homas","thomas--Thomas"))
# ['t', 'T']
# 需求,提取Thomas****man
str = "Thomas is a good man!Thomas is a nice man!Thomas is a very handsome man"
print(re.findall(r"Thomas.*?man",str))
# ['Thomas is a good man', 'Thomas is a nice man', 'Thomas is a very handsome man']

 

4. 特殊形式

*? +? ?? --->> 最小匹配,通常都是尽可能多的匹配,可以使用这种方式解决贪婪匹配。
(?:x) --->> 类似(xyz),单不表示一个组

# 注释:/* part1  */ /* part2 */
print(re.findall(r"//*.*?/*/",r"/* part1  */ /* part2 */"))
# ['/* part1  */', '/* part2 */']

 

5. 实例

  我们在回过头来解决那个电话号码的问题。

def checkPhone2(str):
    # 13912345678
    pat = r"^1(([34578]\d)|(47))\d{8}$"
    res = re.match(pat,str)
    return res

print(checkPhone2("13912345678"))
# <re.Match object; span=(0, 11), match='13912345678'>
print(checkPhone2("139123456785"))
# None
print(checkPhone2("1391234a678"))
# None
print(checkPhone2("23912345678"))
# None
print(checkPhone2("19012345678"))
# None

 

posted @ 2019-05-22 16:49  时海涛|Thomas  阅读(267)  评论(0编辑  收藏  举报