正则表达式元符号,以及基本使用方法

#_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)

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值