#_author:10 #date: 2018/8/17 0017
import re
# 1.()分组 # 2.| 管道符 # 3.?匹配0~1次 # 4.* 匹配 >0 次 # 5.+ 匹配 >1 次 # 6.{} 匹配特定次数 。{3,5}贪婪匹配(默认匹配最长的字符串),{3,5}? 非贪婪匹配(在花括号的后面加一个‘?’) # 7.[] 建立字符分类,匹配方括号中出现的字符。 在方括号内普通的正则表达式的符号不会被解释,加‘^’即为非字符类 # 8.^ 插入符号 ,匹配发生在文本开始处 # 9.$ 美元符,匹配发生在文本结尾 # 10 . 匹配除换行之外的所有字符 (要匹配换行符,需要传入re.DOTALL) # 11 \ 转义字符
#字符分类 ''' \d 0~9的任何数字 \D 除0~9以外的任何字符 \w 任何字母、下划线、或数字字符 \W 除字母、下划线、数字以外的任何字符 \s 空格、制表符或换行符(可以认为是匹配空白字符) \S 除空格、制表符或换行符以外的任何字符 '''
#方法 ''' xRegex = re.compile() 创建了一个 regex对象,compile()第二参数可以选择 re.IGNORECASE re.DOTALL re.verbose xRegex.search() 返回一个 match 对象 xRegex.search().group() xRegex.findall() 返回一个列表 xRegex.sub('string', text) 将匹配的字符串 用string替换
'''
#----------------------------------------------------------------------------------------------------------------------- # '?'通配符,匹配0~1次 # batRegex = re.compile(r'Bat(wo)?man') # mo1 = batRegex.search('The adventures of Batwoman.') # mo2 = batRegex.search('The adventures of Batman.') # # print(mo1.group()) # print(mo2.group())
# '*'通配符,匹配 >0次 # batRegex = re.compile(r'Bat(wo)*man') # mo1 = batRegex.search('Batwowowowowoman') # mo2 = batRegex.search('Batman') # # print(mo1.group()) # print(mo2.group())
#'+'通配符,匹配 >1次 # batRegex = re.compile(r'Bat(wo)+man') # mo1 = batRegex.search('Batwowowowowoman') # mo2 = batRegex.search('Batman') # # print(mo1.group()) # print(mo2.group()) #mo2为None,报错AttributeError: 'NoneType' object has no attribute 'group'.
#‘{}’匹配特定次数 # haregex = re.compile(r'(ha){3}') # mo1 = haregex.search('hahahahaha') # # haregex1 = re.compile(r'(ha){3,5}?') #{3,5}?为非贪婪匹配 # mo2 = haregex1.search('hahahahaahaha') # # print(mo1.group()) # print(mo2.group())
#[] 建立字符分类,匹配方括号出现的字符,[A-Z]表示范围,‘^’取非 # vowelRegex = re.compile(r'[^aeiouAEIOU]') # [^aeiouAEIOU]匹配非(aeiouAEIOU)字符 # mo1 = vowelRegex.findall('Robocop eats baby food. BABY FOOD.') # # print(mo1)
#‘^’从头开始匹配 # beginsWithHello = re.compile(r'^hello') # mo1 = beginsWithHello.search('hello world!') # # print(mo1.group())
#‘$’匹配结束的字符串 # endsWithNumber = re.compile(r'\d$') # mo1 = endsWithNumber.search('Your number is 42') # print(mo1.group())
#‘.’通配字符,可以匹配出了换行之外的所有字符 # atRegex = re.compile(r'.at') # mo1 = atRegex.findall('The cat in the hat sat on the flat mat.') # print(mo1)
#匹配换行符 需要传入 re.DOTALL # newLineRegex = re.compile(r'.*', re.DOTALL) # mo1 = newLineRegex.search('Serve the public trust.\nProtect the innocent.') # print(mo1.group())
#忽略大小写 需要传入re.IGNORECASE 或 re.I
#sub()替换字符串,字符串中的\1将由分组1匹配的文本所替代,也就是正则表达式的(\w)分组。 nameRegex = re.compile(r'Agent (\w)\w*') mo1 = nameRegex.sub(r'\1CENSORED', 'Agent Alice gave the secret documents to Agent Bot') print(mo1)