62_Python正则表达式匹配分组与其他功能函数

一、匹配分组

  • ():表明一个整体,表明分组,然后捕获

二、匹配分组实例

# 导入re模块
import re

tel = "0755-98776754"

# 定义正则表达式
pattern = "(\d{4})-(\d{8})"
result = re.search(pattern, tel)
print(result)    # <re.Match object; span=(0, 13), match='0755-98776754'>
print(result.group())    # 0755-98776754
print(result.group(1))    # 0755
print(result.group(2))    # 98776754
print(result.groups())    # ('0755', '98776754')

三、其他功能函数

  • :表明转义字符,让正则表达式中的字符失去原有的意义
  • .:表明匹配除了换行之外的任意字符,.就表明一个普通的符号,而不是正则表达式中的.
  • |:表明或者,正则表达式1|正则表达式2|正则表达式3,只要满足其中一个正则表达式即可
# 导入re模块
import re

print(re.search("goog\.le", "goog.le"))    # <re.Match object; span=(0, 7), match='goog.le'>
print(re.search("cd|ef|mn", "133cd987"))    # <re.Match object; span=(3, 5), match='cd'>
print(re.search("cd|ef|mn", "133mn987"))    # <re.Match object; span=(3, 5), match='mn'>
  • re.compile() 表明编译正则表达式,用于提高正则匹配的效率
# 导入re模块
import re

str = "010-98766789"
# 定义正则表达式,使用re.compile() 进行编译
pattern = re.compile("(\d{3})-(\d{8})")
print(pattern.findall(str))    # [('010', '98766789')]
  • re.split() 使用指定的正则表达式切割
# 导入re模块
import re

print(re.split("\d", "hello123world987welcome999"))    # ['hello', '', '', 'world', '', '', 'welcome', '', '', '']
  • re.sub() 或者 re.subn() 使用指定的符号替换内容,re.subn() 会现实替换的次数,re.sub不会显示
# 导入re模块
import re

str1 = "今天 天气 好晴朗,这样的 天气适合出去 游玩......"
print(re.sub("\s+", "+++", str1))    # 今天+++天气+++好晴朗,这样的+++天气适合出去+++游玩......
print(re.subn("\s+", "+++", str1))    # ('今天+++天气+++好晴朗,这样的+++天气适合出去+++游玩......', 4)
  • 正则表达式匹配中文
# 导入re模块
import re

chinese = "[u4e00-u9fa5]+"
print(re.search(chinese, "hello 你好 welcome 欢迎光临...."))    # <re.Match object; span=(6, 8), match='你好'>
© 版权声明

相关文章

暂无评论

您必须登录才能参与评论!
立即登录
none
暂无评论...