python之(re)正则表达式下

知识储备:

w 匹配任何字母/数字,下划线

 

正则表达式进阶:

re.match('com', 'comwww.runcomoob')
re.search('dcom', 'www.4comrunoob.5com')
一旦匹配成功,就返回一个match object 对象,对象拥有下列方法;
group()  返回被re 匹配的字符串
start()  返回匹配开始的位置
end()    返回匹配结束的位置(返回结果是索引加1,和分片一样,不包括组的结束位置)
span() 返回一个元组包含匹配(开始,结束)的位置

match() 只匹配起始位置

s = re.match('com', 'www.runcoomoob')
print(s)   #None
print(s.span())   #注意位置,不是0-3

Traceback (most recent call last):
  File "C:/Users/Administrator/PycharmProjects/laonanhai/day6_test/s9.py", line 15, in <module>
    print(s.span())   #注意位置,不是0-3
AttributeError: 'NoneType' object has no attribute 'span'
search() 注意前面的找到则不再往后找
s2 = re.search('com', 'www.runcomoocomb')
print(s2)
#<_sre.SRE_Match object; span=(7, 10), match='com'>  注意前面的找到则不再往后找

  sub/subn 替换  re.sub(pattern, repl, string, max)   max 最大替换次数

s3 = re.sub("g.t", "have", "I get A, I got B, I gut C", 2)
print(s3)    #I have A, I have B, I gut C

s4 = re.subn("g.t", "have", "I get A, I got B, I gut C")
print(s4)    #('I have A, I have B, I gut C', 2)   多了替换次数

 re.split()  分割

s5 = re.split('d+', 'one1two2three3four4')
print(s5)   #['one', 'two', 'three', 'four', '']    注意最后是''

  把那些经常使用的正则表达式编译成正则表达式对象,可以提高一定的效率!

text = 'JGood is a handsome boy, he is cool, clever, and so on...'
regex = re.compile(r'w*oow*')    #regex 是对象
print(regex.findall(text))         #['JGood', 'cool']

正则分组

正则分组:去已经匹配到的数据中再匹配数据(这句话超超重要!!)
举例:

# 无分组
origin = "has fkdghggfhh"
r = re.match("hw+", origin)
print(r.group())      # 获取匹配到的所有结果
print(r.groups())     # 获取模型中匹配到的分组结果
print(r.groupdict())  # 获取模型中匹配到的分组结果

# has
# ()
# {}
import re

m = re.search("(?P<year>[0-9]{4})", "test 2017abc")
print(m)
print(m.group())
print(m.groups())
print(m.group("year"))

输出:

<_sre.SRE_Match object; span=(5, 9), match='2017'>
2017
('2017',)
2017
search()  分组和match()一样,只是找的方式不同
# search()  分组也一样,只是找的方式不同
origin = "has fkdghggfhh"
r = re.match("h(?P<name>w+)", origin)   #给字典添加key,注意括号!!
print(r.group())      # 获取匹配到的所有结果
print(r.groups())     # 获取模型中匹配到的分组结果
print(r.groupdict())  # 获取模型中匹配到的分组中所有执行了key的组

# has
# ('as',)
# {'name': 'as'}


下面是示例,自己看看肯定看得懂的啦


origin = "has fkdghggfhh hal"
#r = re.findall("(hw+)", origin)    #无用分组   ['has', 'hggfhh', 'hal']
#r = re.findall("h(w+)", origin)    #只拿分组里面的东西  ['as', 'ggfhh', 'al']

origin = "hasbbcc fkdghggbbccfhhbbcc halbbcc"
r = re.findall("h(w+)bbc", origin)  #['as', 'ggbbcfhh', 'al']
r = re.findall("h(w+)b(bc)c", origin)
print(r)    #[('as', 'bc'), ('ggbbccfhh', 'bc'), ('al', 'bc')]
 

接下来讲一个蛮重要的点

split() 与正则分组

# 无分组
origin = "hello alex bcd alex lge alex acd 19"
r = re.split("alex", origin, 1)
print(r)        #['hello ', ' bcd alex lge alex acd 19']
# 有分组
origin = "hello alex bcd alex lge alex acd 19"
r1 = re.split("(alex)", origin, 1)
print(r1)   #['hello ', 'alex', ' bcd alex lge alex acd 19']
r2 = re.split("al(ex)", origin, 1)   #重要
print(r2)   #['hello ', 'ex', ' bcd alex lge alex acd 19']

欢迎转发!
This is zcl‘s article! Thanks for your support!
文章出处:http://www.cnblogs.com/0zcl
作者:zcl

原文地址:https://www.cnblogs.com/0zcl/p/5978566.html