re模块

#单个字符匹配,
#普通字符, 一对一完全匹配
import re
# 普通字符
c = re.compile("abc")
f = c.findall("abcfhidabcfioabc")

# 匹配指定范围内任意字符
c = re.compile("[sdc]")
f = c.findall("abcfhisdabcfioabc")
print(f)
# 匹配连续字符
c = re.compile("[a-z]")
f = c.findall("abc,fhgjl1i3sda5b,cf6ioabc")
print(f)

# 匹配所有英文字母,多个连续的片段中间不要加任何多余字符
c = re.compile("[a-zA-Z]")
f = c.findall("abcfAhgjlF1i3sdDa5bcf6ioabc")
print(f)

#匹配相反的字符(除了指定字符范围外的字符)
c = re.compile("[^0-9]")#匹配除了0-9之外的字符
f = c.findall("18946518dsd154fdf1")
print(f)

#匹配除了‘\n’字符之外的任意字符
c = re.compile('.')
f = c.findall("18946518\nndsd1\n54fdf1")
print(f)

# /d :数字字符,匹配数字,相当于[0-9]
c = re.compile("\d")

# 除了数字以外的字符
c = re.compile("\D")
f = c.findall("abcfhgjl1i2233sda5bcf6ioabc")
print(f)

# \w:匹配字(数字,字母,下划线,汉字)
# \W:相反的

# \s:所有的空白字符(\n \t \r 空格)
# \S:相反的内容

# \b:词边界
c = re.compile(r"\bhello")
# f = c.findall("abhellocfhgjl1i2233sda5bcf6ioabc")
f = c.findall("helloab hellocfhgjl,hello1i2233sda5bcf6ioabc")
print(f)

posted on 2018-10-10 20:48  猴哥的编程之路  阅读(206)  评论(0编辑  收藏  举报

导航