python之正则表达式

#-*-coding:utf-8-*-
import re
#python中正则表达式使用re模块访问     例: import re

pattern ="eggspamsausagespam"
print("=============re.match===========================")
#re.match函数来确定字符串的开头是否匹配
#若匹配返回表示匹配的对象,否则返回None
print(re.match(r"egg",pattern))
if re.match(r"egg",pattern):
    print("match")
else:
    print("no")
print("=============re.search===========================")
#re.search函数在字符串中的任何位置找到匹配的模式    
if re.search(r"g",pattern):
    print("search")
else:
    print("no")
print("=============re.findall===========================")    
#re.findall函数返回一个与模式匹配的所有子串的列表
print("=============re.finditer===========================")
print(re.findall(r"pa",pattern))
#re.finditer函数返回一个与模式匹配的所有子串的一个迭代器
print(re.finditer(r"pa",pattern))
print("====match.start=========match.group========match.span===================")
match = re.search(r"pa",pattern)
print(match.group())
print(match.start())
print(match.end())
print(match.span())
print("=============re.sub===========================")
#re.sub(pattern,repl,string,max=0)
#此方法用repl替换字符串中所有出现的模式,除非提供max限定的修改限定
#sub方法返回修改后的字符串
str ="my name is loen. hi loen."
pattern =r"loen"
newstr =re.sub(pattern,"amy",str)
print(newstr)
print("=============元字符^和$和.===========================")
#
#元字符
#
#元字符.匹配任何一个非换行的字符
#元字符^和$分别匹配字符串的开始和结束
#模式"^gr.y$"表示字符串应该以gr开头,然后跟随一个任何字符,除了换行符,并以y结尾
#例:
pat = r"^gr.y$"
if re.match(pat,"grey"):
    print("grey")
else:
    print("no")
if re.match(pat,"grdfy"):
    print("grdfy")
else:
    print("no")
if re.match(pat,"greys"):
    print("grey")
else:
    print("no")
print("=============字符类  [aeiou]===========================")   
#
#字符类
#
#字符类提供了一种只匹配特定字符集中的一个的方法
#通过将匹配的字符放在方括号内来创建字符类。
#搜索函数中的模式[aeiou]匹配包含任何一个定义的字符的所有字符串
#例:
pat = r"[aeiou]"
if re.search(pat,"grey"):
    print("search1")
else:
    print("no")
if re.search(pat,"grdxcvbgfuy"):
    print("search2")
else:
    print("no")
if re.search(pat,"grjljny"):
    print("search3")
else:
    print("no")
print("=============字符类 [a-z][0-9]==========================")    
#
#字符类
#
#[a-z]匹配任何小写字母字符
#[G-P]匹配从G到P的任何大写字符
#[0-9]匹配任何数字
#例:
pat = r"[A-Z][A-Z][0-9]"
if re.search(pat,"AF3"):
    print(1)
else:
    print("no")
if re.search(pat,"A3"):
    print(2)
else:
    print("no")
if re.search(pat,"asd"):
    print(3)
else:
    print("no")
print("=============字符类 [^A-Z]===========================")    
#
#字符类
#
#在字符类的开头放置一个^来翻转它,这使得它匹配除包含的字符之外的任何字符
#[^A-Z]匹配不包括大写字母的所有字符
#例:
pat = r"[^A-Z]"
if re.search(pat,"this is all quiet"):
    print("match 1")
else:
    print("no")
if re.search(pat,"This is all quiet"):
    print("match 2")
else:
    print("no")
if re.search(pat,"THISISALLQUIT"):
    print("match 3")
else:
    print("no")    

print("=============字符类  $和 .和^  ===========================")
#
#字符类
#
#其他元字符($和.)在字符类中没有意义
#元字符^没有意义,除非他是一个字符类中的第一个字符

pat = r"^a[a^]*"
if re.search(pat,"a^thisisllquiet"):
    print("match 1")
else:
    print("no")
if re.search(pat,"aThis is all quiet"):
    print("match 2")
else:
    print("no")
if re.search(pat,"vcasd"):
    print("match 3")
else:
    print("no")    

print("=============元字符  * ===========================")
#
#元字符
#
#元字符*表示“零次或者多次重复以前的事情”
#*号前可以是一个单独的字符,一个类,或一组括在括号中的字符

pat = r"^ess(sw)*"
if re.match(pat,"essswsd"):
    print("match 1")
else:
    print("no")
if re.match(pat,"essaThis is all quiet"):
    print("match 2")
else:
    print("no")
if re.match(pat,"vcasd"):
    print("match 3")
else:
    print("no") 

print("=============元字符  + ===========================")
#
#元字符
#
#元字符+与*非常相似,不同之处在于+是“一个或多个重复”
#而*是“零个或多个重复”

pat = r"g+"
if re.match(pat,"gwsd"):
    print("match 1")
else:
    print("no")
if re.match(pat,"gggggessaThis isuiet"):
    print("match 2")
else:
    print("no")
if re.match(pat,"vcasd"):
    print("match 3")
else:
    print("no")
    
print("=============元字符  ? ===========================")
#
#元字符
#
#元字符?匹配“零次重复或一次重复”
#
pat = r"ice(-)?cream"
if re.match(pat,"ice-cream"):
    print("match 1")
else:
    print("no")
if re.match(pat,"icecream"):
    print("match 2")
else:
    print("no")
if re.match(pat,"vcasd"):
    print("match 3")
else:
    print("no")
if re.match(pat,"ice--ice"):
    print("match 3")
else:
    print("no")
    
print("=============元字符  {x,y}===========================")
#
#元字符
#
#大括号可以用来表示两个数字之间的重复次数
#正则表达式{x,y}表示“在x和y之间重复某事”
#因此{0,1}与?相同
#大括号如果第一个数字缺失,则将其视为零,如果第二个数字缺失,则被认为是无限的

pat = r"9{1,3}$"
if re.match(pat,"9"):
    print("match 1")
else:
    print("no")
if re.match(pat,"99"):
    print("match 2")
else:
    print("no")
if re.match(pat,"999"):
    print("match 3")
else:
    print("no")
if re.match(pat,"999999"):
    print("match 3")
else:
    print("no")

print("=============分组  组的创建  组可做为元字符的参数===========================")
#
#分组
#
#可以通过用圆括号围绕正则表达式的一部分来创建组
#这意味着一个组可以作为元字符的参数,如*和?
#
#(spam)代表下面示例模式中的一个组

pat = r"egg(spam)*"
if re.match(pat,"egg"):
    print("match 1")
else:
    print("分组no")
if re.match(pat,"eggspamspamspamegg"):
    print("match 2")
else:
    print("no")
if re.match(pat,"spam"):
    print("match 3")
else:
    print("no---------------------------")
print("=============分组 组的访问 group===========================")
#
#分组
#
#可以使用组功能访问匹配组中的内容
#可以调用group(0)或者group()返回整个匹配
#调用group(n),n要大于0,返回匹配的第n个组
#groups()返回所有匹配的分组
#组可以嵌套
#例:

pat = r"a(bc)(de)(f(g)h)i"
match = re.match(pat,"abcdefghijklmnop")

print(match)
if match:
    print(match.group())
    print(match.group(0))
    print(match.group(1))
    print(match.group(2))
    print(match.groups())
    
print("===========分组:命名组和非捕获组===========================")
#
#分组
#
#有几种特殊的群组
#两个有用的命名组和非捕获组
#命名组格式为  (?P<name>...),
#   其中name是组的名称
#   ...是内容
#   他们的表现与正常组完全相同,除了可以通过group(name)代替编号访问
#非捕获组格式为  (?:...)
#    它们不能通过组方法访问,所以可以将它们添加到现有的正则表达式中而不会破坏编号
#例:

pat =r"(?P<first>abc)(?:def)(ghi)"
match = re.match(pat,"abcdefghi")
if match:
    print(match.group("first"))
    print(match.groups())

    
print("=============元字符  | ===========================")
#
#元字符
#
#另一个重要的元字符是|
#意思是与,比如red|blue匹配red或者blue
#例:

pat = r"gr(a|e)y"
match = re.match(pat,"gray")
if match:
    print("Match 1")
match = re.match(pat,"grey")
if match:
    print("Match 2")
match = re.match(pat,"griy")
if match:
    print("Match 3")


print("================特殊序列  \  =====================")
#
#特殊序列  \
#
#在正则表达式中可以使用各种特殊的序列,它们写成反斜杠,然后是一个字符。
#
#一个有用的特殊序列是反斜杠和1到99之间的数字。例如\1或\19.这匹配该数字的组的表达式
#(注意,"(.+) \1"和"(.+)(.+)"不一样,因为\1是指第一组的子表达式,他是匹配的表达式本身)
#例:

pat = r"(.+) \1"
match = re.match(pat,"word word")
if match:
    print("word word")
match = re.match(pat,"?! ?!")
if match:
    print("?! ?!")
match = re.match(pat,"abc cde")
if match:
    print("abc cde")  
match = re.match(pat,"a")
if match:
    print("abc abc")

print("=============特殊序列 \d  \s和\w \D  \S和\W===========================")
#
#
#特殊序列:
#
#   \d  匹配   数字
#   \s  匹配   空格
#   \w  匹配   单词字符
#  在ASCII模式下:
#      \d    匹配   [0-9]
#      \s    匹配   [\t\n\r\f\v]
#      \w    匹配   [a-zA-Z0-9]
#
#  在Unicode模式下;
#      \w    匹配   带有重音的字母
#
#大写字母\D \S和\W这些特殊序列的版本意味着与小写字母相反的版本如:
#       \D  匹配   任何不是数字的东西
#
#例:
#(\D+\d)匹配一个或多个非数字后跟一个数字

pat = r"(\D+\d)"
match = re.match(pat,"Hi 999")
if match:
    print("(\D+\d)Hi 999")
match =re.match(pat,"1,23,456!")
if match:
    print("(\D+\d)1,23,456!")
match = re.match(pat," ! $?")
if match:
    print("(\D+\d) ! $?")

print("=============特殊序列  \A,\ 和 \b ===========================")
#
#特殊序列
#
#\A和\Z   分别匹配字符串的开头和结尾
#\b       匹配\w和\W字符之间的空字符串,或\w字符和字符串的开始或结尾。非正式地,
#         它代表了单词之间的界限
#\B       匹配其他地方的空字符串   
#
#例:
#"\b(cat)\b"基本上与单词边界包围的单词"cat"匹配。

pat = r"\b(cat)\b"
match = re.search(pat,"The cat sat!")
if match:
    print("\b(cat)\b The cat sat!")
match = re.search(pat,"Wes>cat<tered?")
if match:
    print("\b(cat)\b Wes>cat<tered")
match = re.search(pat,"Wescattered.")
if match:
    print("\b(cat)\b  Wescattered")

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值