正则表达式简略笔记

说明:之前提到正则表达式就头大,每次也都是看个开头就不看了,坚持不下去,这次终于完整地看完一次文档,加上之前多多少少看过的,对正则表达式算是有了整体的了解。这次笔记主要结合一培训班的课件,以Python的re模块为主。

简略笔记:

1,正则表达式(Regular Expression),正则表达式使⽤单个字符串来描述、匹配⼀系列匹配某个句法规则的字符串。简单说就是,这是一个规则,它体现为一个字符串,可以提取出符合规则的内容。

2,用途:筛选,查找,匹配。

3,学习方法:以我自己的经验来看,只是简单的看规则简直难受,还是觉得结合实例,一边练习一边记忆比较好,然后就是多练习练习可以达到很好的效果。

4,Python的正则表达式模块:Re,match⽅法进⾏匹配操作,group⽅法来提取数据

字符

例子:

import re

ret = re.match('.', 'a')
ret.group()    #  'a'
ret = re.match(".","b")
ret.group()    #  'b'
ret = re.match(".","M")
ret.group()    #  'M'

# 如果hello的⾸字符⼩写,那么正则表达式需要⼩写的h
ret = re.match("h","hello Python")
ret.group()    # 'h'

# 如果hello的⾸字符⼤写,那么正则表达式需要⼤写的H
ret = re.match("H","Hello Python")
ret.group()    #  'H'

# ⼤⼩写h都可以的情况
ret = re.match("[hH]","hello Python")
ret.group()    #  'h'
ret = re.match("[hH]","Hello Python")
ret.group()    #  'H'

# 匹配0到9第⼀种写法
ret = re.match("[0123456789]","7Hello Python")
ret.group()    #   '7'

# 匹配0到9第⼆种写法
ret = re.match("[0-9]","7Hello Python")
ret.group()   #  '7'

# 普通的匹配⽅式
ret = re.match("S1季中赛","S1季中赛冠军")
print ret.group()   # S1季中赛
ret = re.match("S2季中赛","S2季中赛冠军")
print ret.group()   #  S2季中赛
ret = re.match("S3季中赛","S3季中赛冠军")
print ret.group()   #  S3季中赛

# 使⽤\d进⾏匹配
ret = re.match("S1季中赛","S1季中赛冠军")
print ret.group()   # S1季中赛

原始字符串

正则表达式⾥使⽤"\"作为转义字符,假如你需要匹配⽂本中的字符"\",那么使⽤编程语⾔表示的正则表达式⾥将需要4个反斜杠"\\":前两个和后两个分别⽤于在编程语⾔⾥转义成反斜杠,转换成两个反斜杠后再在正则表达式⾥转义成⼀个反斜杠。Python中字符串前面加上r表示原生字符串可以改善这个问题。

例子:

import re


ret = re.match("c:\\\\", "c:\\a\\b\\c").group()
print(ret)  # 'c:\\'
ret = re.match(r"c:\\a", "c:\\a\\b\\c").group()
print(ret)  #  'c:\a'


# 使用原生字符串
ret = re.match(r"c:\\a", "c:\\a\\b\\c").group()
print(ret)  # 'c:\a'

数量

例子:

import re


ret = re.match("[A-Z][a-z]*","Aabcdef")
ret.group()    #  'Aabcdef'

ret = re.match("[a-zA-Z_]+[\w_]*","name1")
ret.group()   # 'name1'
ret = re.match("[a-zA-Z_]+[\w_]*","_name")
ret.group()   #  '_name'

ret = re.match("[1-9]?[0-9]","33")
ret.group()   #  '33'
ret = re.match("[1-9]?[0-9]","09")
ret.group()   #  '0'


ret = re.match("[a-zA-Z0-9_]{6}","12a3g45678")
ret.group()   #  '12a3g4'
ret = re.match("[a-zA-Z0-9_]{8,20}","1abcde23s34455ff66")
ret.group()   #  '1abcde23s34455ff66'

边界:

例子:

import re


ret = re.match("[\w]{4,20}@163\.com$", "[email protected]").group()
print(ret)   #  '[email protected]'

ret = re.match(r".*\bver\b", "ab cde fgh").group()
print(ret)   #  'ab cde'

匹配分组:

例子:

import ret


ret = re.match("\w{4,20}@163\.com", "[email protected]")
ret.group()     
ret = re.match("\w{4,20}@(163|126|qq)\.com", "[email protected]")
ret.group()
ret = re.match("\w{4,20}@(163|126|qq)\.com", "[email protected]")
ret.group()
ret = re.match("\w{4,20}@(163|126|qq)\.com", "[email protected]")
ret.group()



#   "[email protected]"
#   "[email protected]"
#   "[email protected]"
#   AttributeError

# 通过引⽤分组中匹配到的数据即可,但是要注意是元字符串,即类似 r""这种格式
ret = re.match(r"<([a-zA-Z]*)>\w*</\1>", "<html>mm</html>")
ret.group()
# 因为2对<>中的数据不⼀致,所以没有匹配出来
ret = re.match(r"<([a-zA-Z]*)>\w*</\1>", "<html>mm</htmlbalabala>")
ret.group()


#  "<html>mm</html>"
#  AttributeError


ret = re.match(r"<(?P<name1>\w*)><(?P<name2>\w*)>.*</(?P=name2)></(?P=name1)>", "<html><h1>www.python.org</h1></html>")
ret.group()
ret = re.match(r"<(?P<name1>\w*)><(?P<name2>\w*)>.*</(?P=name2)></(?P=name1)>", "<html><h1>www.python.org</h2></html>")
ret.group()


#   "<html><h1>www.python.org</h1></html>"
#   AttributeError  

贪婪和非贪婪:

Python⾥数量词默认是贪婪的(在少数语⾔⾥也可能是默认⾮贪婪),总是尝试匹配尽可能多的字符;⾮贪婪则相反,总是尝试匹配尽可能少的字符。在"*","?","+","{m,n}"后⾯加上?,使贪婪变成⾮贪婪。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章