python 正则表达式

#test_RegEX
import re

#matching string
str_a = "cat"
str_b = 'bird'
str_c = "dog is catching cat"
print(str_a in str_c)
print(str_b in str_c)
#True
#False

#regular expression
str_a = "cat"
str_b = 'bird'
str_c = "dog is catching cat"
print(re.search(str_a,str_c))
print(re.search(str_b,str_c))
#<re.Match object; span=(6, 9), match='cat'>
#None

#匹配多种可能
str_d = r'd[oa]g'
print(re.search(str_d,str_c))
#<re.Match object; span=(0, 3), match='dog'>

#匹配更多种可能
print(re.search(r"r[A-Z]n",'dog runs to cat'))  
#None
print(re.search(r"r[a-z]n",'dog runs to cat'))
#<re.Match object; span=(4, 7), match='run'>
print(re.search(r"r[0-9]n",'dog r2ns to cat'))
#<re.Match object; span=(4, 7), match='r2n'>
print(re.search(r"r[0-9a-z]n",'dog runs to cat'),'\n')
#<re.Match object; span=(4, 7), match='run'>

#特殊种类类匹配

#数字
#\d 所有数字
#\D 所有非数字
print(re.search(r'r\dn','run r4n'))
#<re.Match object; span=(4, 7), match='r4n'>
print(re.search(r'r\Dn','run r4n'),'\n')
#<re.Match object; span=(0, 3), match='run'>

#空白符
#\s 所有空白符 [\t \n \r \f \v]
#\S 相反
print(re.search(r'r\sn','r\nn r4n'))
#<re.Match object; span=(0, 3), match='r\nn'>
print(re.search(r'r\Sn','r\nn r4n'),'\n')
#<re.Match object; span=(4, 7), match='r4n'> 

#\w 所有字母数字 ”_“
#\W 相反
print(re.search(r'r\wn','r\nn r4n'))
#<re.Match object; span=(4, 7), match='r4n'>
print(re.search(r'r\Wn','r\nn r4n'),'\n')
#<re.Match object; span=(0, 3), match='r\nn'> 

#空白字符
#\b 所有空白符,只有在单词开始和结尾
#\B 空白符,不管单词开始结束都可匹配
print(re.search(r'\bruns\b','dog runs to cat'))
print(re.search(r'\B runs \B','dog     runs     to cat'),'\n')
# <re.Match object; span=(4, 8), match='runs'>
# <re.Match object; span=(7, 13), match=' runs '> 



#\\ 匹配\
#. 匹配任何 除了\n  通配符
print(re.search(r'runs\\','dog runs\ to cat'))
print(re.search(r'r.n','dog r@ns\ to cat'),'\n')
# <re.Match object; span=(4, 9), match='runs\\'>
# <re.Match object; span=(4, 7), match='r@n'> 


#^  句首
#$ 句尾
print(re.search(r'^dog','dog runs\ to cat'))
print(re.search(r'c.t$','dog r@ns\ to cat'),'\n')
# <re.Match object; span=(0, 3), match='dog'>
# <re.Match object; span=(13, 16), match='cat'>

#?是否出现
print(re.search(r'Mon(day)?','Monday'))
print(re.search(r'Mon(day)?','Mon'),'\n')
# <re.Match object; span=(0, 6), match='Monday'>
# <re.Match object; span=(0, 3), match='Mon'> 

#多行匹配
string = """
sfsf
I sff
"""
print(re.search(r'^I',string))
print(re.search(r'^I',string,flags=re.M),'\n')
# None
# <re.Match object; span=(6, 7), match='I'>

#* 0次或者多次
#+ 1次或者多次
print(re.search(r'ab*','a'))
print(re.search(r'ab*','abbbbbb'))
# <re.Match object; span=(0, 1), match='a'>
# <re.Match object; span=(0, 7), match='abbbbbb'>


print(re.search(r'ab+','a'))
print(re.search(r'ab+','abbbbbb'),'\n')
# None
# <re.Match object; span=(0, 7), match='abbbbbb'> 


#可选次数
print(re.search(r'ab{2,10}','a'))
print(re.search(r'ab{2,10}','abbbbbbb'),'\n')
# None
# <re.Match object; span=(0, 8), match='abbbbbbb'> 

#group
match = re.search(r'(\d+), Date:(.+)','ID: 021523, Date:Feb/12/2017')  #必须保证前后的格式一致'(\d+), Date: (.+)','ID: 021523, Date:Feb/12/2017'
print(match.group())
print(match.group(1))
print(match.group(2),'\n')
#021523, Date:Feb/12/2017
# 021523
# Feb/12/2017
match = re.search(r'(?P<id>\d+), Date:(?P<data>.+)','ID: 021523, Date:Feb/12/2017')  #必须保证前后的格式一致'(\d+), Date: (.+)','ID: 021523, Date:Feb/12/2017'
print(match.group())
print(match.group('id'))
print(match.group('data'),'\n')

#寻找所有匹配
#findall()
#| 或者
print(re.findall(r'r[au]n','ran run ren'))
print(re.findall(r'r(a|u)n','ran run ren'),'\n')

#替换
#re.sub()
print(re.sub(r'r[au]n','catches','dogs run to cat'),'\n')
# dogs catches to cat 

#分裂
#re.split()
print(re.split(r"[,;\.]",'a:b,c\d.e'),'\n')
# ['a:b', 'c\\d', 'e']

#compile
compile_re = re.compile(r'r[ua]n')
print(compile_re.search('dog run to cat'))
#<re.Match object; span=(4, 7), match='run'>








 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值