欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Python-正则表达式的元字符

程序员文章站 2022-07-10 08:21:28
...
import re

print("--------------匹配单个字符与数字--------------")
'''
r
.  匹配除换行符以外的任意字符
[0123456789]  []是字符集合,表示匹配方括号中所包含的任意一个字符
[sunck]       匹配's','u','n','c','k'中任意一个字符
[a-z]         匹配任意小写字母
[A-Z]         匹配任意大写字母
[0-9]         匹配任意数字,类似[0123456789]
[0-9a-zA-Z]   匹配任意的数字和字母(注意之间没有空格)
[0-9a-zA-Z_]  匹配任意的数字、字母、下划线(注意之间没有空格)
[^sunck]      匹配除了sunck这几个字母以外的所有字符
^称为脱字符,表示不匹配集合中的字符
[^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(".","sunck is a good man "))
print(re.search("[0123456789]","sunck is a good man 3"))
print(re.search("[sunck]","sunck is a good man 3"))
print(re.findall("[\d]","sunck is a good man 3"))
print(re.search("\D","sunck is a good man 3"))
print(re.search("\w","_sunck is a good man 3"))
print(re.findall("\d","_sunck is 6a go80d man 3" ))


print("-------------------锚字符(边界字符)-------------------")
'''
^        行首匹配,和[]里的^不是一个意思
$        行尾匹配  

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

print(re.search("^sunck","sunck si a good man"))
print(re.search("sunck$","sunck si a good man"))
print(re.search("man$","sunck si a good man"))
print(re.search("^man$","sunck si a good man"))

print(re.search("^sunck","sunck si a good man\nsunck is a nice man"))
print(re.search("A\sunck","sunck si a good man\nsunck is a nice man"))
print(re.search("^sunck","sunck si a good man\nsunck is a nice man",re.M))
print(re.search("A\sunck","sunck si a good man\nsunck is a nice man",re.M))

print(re.search(r"er\b","never"))
print(re.search(r"er\b","nerve"))
print(re.search(r"er\B","never"))
print(re.search(r"er\B","nerve"))





print("-------------------------匹配多个字符-------------------------")

'''
说明:下方的x,y,z,n均为假设的普通字符,不是正则表达式的元字符(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个。注意:n <= m
x|y   |表示或,匹配的是x或y
'''
print(re.findall(r"(sunck)","sunck is a good man ,sunck is a nice man "))
print(re.findall(r"0?","sunck is a good man ,sunck is a nice man "))

print(re.findall(r"a?","aaa"))   # 非贪婪匹配(尽可能少的匹配)
print(re.findall(r"a*","aaabaa")) # 贪婪匹配(尽可能多的匹配)
print(re.findall(r".*","aaabaa"))# 贪婪匹配(尽可能多的匹配)
print(re.findall(r"a+","aaa"))# 贪婪匹配(尽可能多的匹配)
print(re.findall(r"a+","aaabaa"))# 贪婪匹配(尽可能多的匹配)

print(re.findall(r"a{3}","aa"))
print(re.findall(r"a{3}","aaaa"))
print(re.findall(r"a{3}","aaaaa"))
print(re.findall(r"a{3}","aaabaa"))

print(re.findall(r"a{3,}","aaaaa"))
print(re.findall(r"a{3,}","aaa"))
print(re.findall(r"a{3,}","aa"))
print(re.findall(r"a{3,}","aaaaabaaa"))# 贪婪匹配(尽可能多的匹配)

print(re.findall(r"a{3,6}","aaaaabaaa"))

print(re.findall(r"((s|S)unck)","sunck--Sunck"))
print(re.findall(r"((s|S)unck)","sunck--SuNck"))

# 需求,提取sunck……man,
str = "sunck is a good man!sunck is a nice man!sunck is a very handsome man "
print(re.findall(r"^sunck.*man",str))
print(re.findall(r"^sunck.*man$",str))

print("-------------------特殊-------------------------")
'''
*?  +?  x?   最小匹配   通常都是尽快可能多的匹配,可以用这种方式解决贪婪匹配
(?:x)        类似(xyz),但不表示一个组
'''
# 注释:  /* part1 */  /*  part2  /*
print(re.findall(r"//*.*/*/",r"/*  part1  */  /*  part2  */"))

print(re.findall(r"(^sunck(.*?)man$)",str))

import re

print("--------------匹配单个字符与数字--------------")
'''
r
.  匹配除换行符以外的任意字符
[0123456789]  []是字符集合,表示匹配方括号中所包含的任意一个字符
[sunck]       匹配's','u','n','c','k'中任意一个字符
[a-z]         匹配任意小写字母
[A-Z]         匹配任意大写字母
[0-9]         匹配任意数字,类似[0123456789]
[0-9a-zA-Z]   匹配任意的数字和字母(注意之间没有空格)
[0-9a-zA-Z_]  匹配任意的数字、字母、下划线(注意之间没有空格)
[^sunck]      匹配除了sunck这几个字母以外的所有字符
^称为脱字符,表示不匹配集合中的字符
[^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(".","sunck is a good man "))
print(re.search("[0123456789]","sunck is a good man 3"))
print(re.search("[sunck]","sunck is a good man 3"))
print(re.findall("[\d]","sunck is a good man 3"))
print(re.search("\D","sunck is a good man 3"))
print(re.search("\w","_sunck is a good man 3"))
print(re.findall("\d","_sunck is 6a go80d man 3" ))


print("-------------------锚字符(边界字符)-------------------")
'''
^        行首匹配,和[]里的^不是一个意思
$        行尾匹配  

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

print(re.search("^sunck","sunck si a good man"))
print(re.search("sunck$","sunck si a good man"))
print(re.search("man$","sunck si a good man"))
print(re.search("^man$","sunck si a good man"))

print(re.search("^sunck","sunck si a good man\nsunck is a nice man"))
print(re.search("A\sunck","sunck si a good man\nsunck is a nice man"))
print(re.search("^sunck","sunck si a good man\nsunck is a nice man",re.M))
print(re.search("A\sunck","sunck si a good man\nsunck is a nice man",re.M))

print(re.search(r"er\b","never"))
print(re.search(r"er\b","nerve"))
print(re.search(r"er\B","never"))
print(re.search(r"er\B","nerve"))





print("-------------------------匹配多个字符-------------------------")

'''
说明:下方的x,y,z,n均为假设的普通字符,不是正则表达式的元字符(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个。注意:n <= m
x|y   |表示或,匹配的是x或y
'''
print(re.findall(r"(sunck)","sunck is a good man ,sunck is a nice man "))
print(re.findall(r"0?","sunck is a good man ,sunck is a nice man "))

print(re.findall(r"a?","aaa"))   # 非贪婪匹配(尽可能少的匹配)
print(re.findall(r"a*","aaabaa")) # 贪婪匹配(尽可能多的匹配)
print(re.findall(r".*","aaabaa"))# 贪婪匹配(尽可能多的匹配)
print(re.findall(r"a+","aaa"))# 贪婪匹配(尽可能多的匹配)
print(re.findall(r"a+","aaabaa"))# 贪婪匹配(尽可能多的匹配)

print(re.findall(r"a{3}","aa"))
print(re.findall(r"a{3}","aaaa"))
print(re.findall(r"a{3}","aaaaa"))
print(re.findall(r"a{3}","aaabaa"))

print(re.findall(r"a{3,}","aaaaa"))
print(re.findall(r"a{3,}","aaa"))
print(re.findall(r"a{3,}","aa"))
print(re.findall(r"a{3,}","aaaaabaaa"))# 贪婪匹配(尽可能多的匹配)

print(re.findall(r"a{3,6}","aaaaabaaa"))

print(re.findall(r"((s|S)unck)","sunck--Sunck"))
print(re.findall(r"((s|S)unck)","sunck--SuNck"))

# 需求,提取sunck……man,
str = "sunck is a good man!sunck is a nice man!sunck is a very handsome man "
print(re.findall(r"^sunck.*man",str))
print(re.findall(r"^sunck.*man$",str))

print("-------------------特殊-------------------------")
'''
*?  +?  x?   最小匹配   通常都是尽快可能多的匹配,可以用这种方式解决贪婪匹配
(?:x)        类似(xyz),但不表示一个组
'''
# 注释:  /* part1 */  /*  part2  /*
print(re.findall(r"//*.*/*/",r"/*  part1  */  /*  part2  */"))

print(re.findall(r"(^sunck(.*?)man$)",str))


import re

print("--------------匹配单个字符与数字--------------")
'''
r
.  匹配除换行符以外的任意字符
[0123456789]  []是字符集合,表示匹配方括号中所包含的任意一个字符
[sunck]       匹配's','u','n','c','k'中任意一个字符
[a-z]         匹配任意小写字母
[A-Z]         匹配任意大写字母
[0-9]         匹配任意数字,类似[0123456789]
[0-9a-zA-Z]   匹配任意的数字和字母(注意之间没有空格)
[0-9a-zA-Z_]  匹配任意的数字、字母、下划线(注意之间没有空格)
[^sunck]      匹配除了sunck这几个字母以外的所有字符
^称为脱字符,表示不匹配集合中的字符
[^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(".","sunck is a good man "))
print(re.search("[0123456789]","sunck is a good man 3"))
print(re.search("[sunck]","sunck is a good man 3"))
print(re.findall("[\d]","sunck is a good man 3"))
print(re.search("\D","sunck is a good man 3"))
print(re.search("\w","_sunck is a good man 3"))
print(re.findall("\d","_sunck is 6a go80d man 3" ))


print("-------------------锚字符(边界字符)-------------------")
'''
^        行首匹配,和[]里的^不是一个意思
$        行尾匹配  

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

print(re.search("^sunck","sunck si a good man"))
print(re.search("sunck$","sunck si a good man"))
print(re.search("man$","sunck si a good man"))
print(re.search("^man$","sunck si a good man"))

print(re.search("^sunck","sunck si a good man\nsunck is a nice man"))
print(re.search("A\sunck","sunck si a good man\nsunck is a nice man"))
print(re.search("^sunck","sunck si a good man\nsunck is a nice man",re.M))
print(re.search("A\sunck","sunck si a good man\nsunck is a nice man",re.M))

print(re.search(r"er\b","never"))
print(re.search(r"er\b","nerve"))
print(re.search(r"er\B","never"))
print(re.search(r"er\B","nerve"))





print("-------------------------匹配多个字符-------------------------")

'''
说明:下方的x,y,z,n均为假设的普通字符,不是正则表达式的元字符(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个。注意:n <= m
x|y   |表示或,匹配的是x或y
'''
print(re.findall(r"(sunck)","sunck is a good man ,sunck is a nice man "))
print(re.findall(r"0?","sunck is a good man ,sunck is a nice man "))

print(re.findall(r"a?","aaa"))   # 非贪婪匹配(尽可能少的匹配)
print(re.findall(r"a*","aaabaa")) # 贪婪匹配(尽可能多的匹配)
print(re.findall(r".*","aaabaa"))# 贪婪匹配(尽可能多的匹配)
print(re.findall(r"a+","aaa"))# 贪婪匹配(尽可能多的匹配)
print(re.findall(r"a+","aaabaa"))# 贪婪匹配(尽可能多的匹配)

print(re.findall(r"a{3}","aa"))
print(re.findall(r"a{3}","aaaa"))
print(re.findall(r"a{3}","aaaaa"))
print(re.findall(r"a{3}","aaabaa"))

print(re.findall(r"a{3,}","aaaaa"))
print(re.findall(r"a{3,}","aaa"))
print(re.findall(r"a{3,}","aa"))
print(re.findall(r"a{3,}","aaaaabaaa"))# 贪婪匹配(尽可能多的匹配)

print(re.findall(r"a{3,6}","aaaaabaaa"))

print(re.findall(r"((s|S)unck)","sunck--Sunck"))
print(re.findall(r"((s|S)unck)","sunck--SuNck"))

# 需求,提取sunck……man,
str = "sunck is a good man!sunck is a nice man!sunck is a very handsome man "
print(re.findall(r"^sunck.*man",str))
print(re.findall(r"^sunck.*man$",str))

print("-------------------特殊-------------------------")
'''
*?  +?  x?   最小匹配   通常都是尽快可能多的匹配,可以用这种方式解决贪婪匹配
(?:x)        类似(xyz),但不表示一个组
'''
# 注释:  /* part1 */  /*  part2  /*
print(re.findall(r"//*.*/*/",r"/*  part1  */  /*  part2  */"))

print(re.findall(r"(^sunck(.*?)man$)",str))

# 测试如下:

--------------匹配单个字符与数字--------------

  PycharmProjects/untitled/正则表达式的元字符.py", line 119, in <module>
<_sre.SRE_Match object; span=(0, 1), match='s'>
    print(re.findall(r"(^sunck(.*?)man$)",str))
<_sre.SRE_Match object; span=(20, 21), match='3'>
  File "E:\lib\re.py", line 222, in findall
<_sre.SRE_Match object; span=(0, 1), match='s'>
    return _compile(pattern, flags).findall(string)
['3']
  File "E:\lib\re.py", line 301, in _compile
<_sre.SRE_Match object; span=(0, 1), match='s'>
    p = sre_compile.compile(pattern, flags)
<_sre.SRE_Match object; span=(0, 1), match='_'>
  File "E:\lib\sre_compile.py", line 562, in compile
['6', '8', '0', '3']
    p = sre_parse.parse(p, flags)
-------------------锚字符(边界字符)-------------------
  File "E:\lib\sre_parse.py", line 869, in parse
<_sre.SRE_Match object; span=(0, 5), match='sunck'>
    raise source.error("unbalanced parenthesis")
None
sre_constants.error: unbalanced parenthesis at position 16
<_sre.SRE_Match object; span=(16, 19), match='man'>
None
<_sre.SRE_Match object; span=(0, 5), match='sunck'>
None
<_sre.SRE_Match object; span=(0, 5), match='sunck'>
None
<_sre.SRE_Match object; span=(3, 5), match='er'>
None
None
<_sre.SRE_Match object; span=(1, 3), match='er'>
-------------------------匹配多个字符-------------------------
['sunck', 'sunck']
['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '']
['a', 'a', 'a', '']
['aaa', '', 'aa', '']
['aaabaa', '']
['aaa']
['aaa', 'aa']
[]
['aaa']
['aaa']
['aaa']
['aaaaa']
['aaa']
[]
['aaaaa', 'aaa']
['aaaaa', 'aaa']
[('sunck', 's'), ('Sunck', 'S')]
[('sunck', 's')]
['sunck is a good man!sunck is a nice man!sunck is a very handsome man']
[]
-------------------特殊-------------------------
['/*  part1  */  /*  part2  */']


随笔:别拿自己的无知当个性!

相关标签: re