Python中正则表达式的用法总结

正则表达式很神奇啊

# -*- coding:utf-8 -*-

import re

def print_match_res(res):
    """打印匹配对象内容"""
    if res is not None:
        print(res.group())
    else:
        print(None)


# 两种匹配方式:
pattern="[A-Z][a-z]+"
# 一、使用re模块函数进行匹配
res=re.match(pattern,"Tom is a good boy")         # 匹配,返回匹配对象
print(type(res))
print(res.group())

# 二、使用预编译后的正则表达式对象的方法进行匹配
obj_pattern=re.compile(pattern)     # 预编译,返回正则表达式对象
print(type(obj_pattern))
res=obj_pattern.match("Tom is a good boy")        # 匹配,返回匹配对象
print(type(res))
print(res.group())


# 匹配对象的group()和groups()方法
pattern="\d{3}-\d{5}"
obj_pattern=re.compile(pattern)
res=obj_pattern.search("家庭电话:000-88886")
print(res.group())      # 返回整个匹配或特定子组
print(res.groups())     # 返回包含全部子组的元组


# match():从起始部分开始匹配,如果成功,返回匹配对象;失败,返回None。只匹配一次
pattern="my"
# res=re.compile(pattern).match("my name is li")
res=re.match(pattern,"my name is li")
print_match_res(res)

# search(): 从任意位置开始匹配,如果成功,返回匹配对象;失败,返回None。只匹配一次
pattern="my"
# res=re.compile(pattern).search("it's my dog")
res=re.search(pattern,"my name is li")
print_match_res(res)


# 查找全部
# findall(),finditer()
res=re.findall(r"th\w+","This and that",re.I)
print(res)
res=re.finditer(r"th\w+","This and that",re.I)
print(res)
print(next(res).group(),next(res).group())


# 替换
# sub(),subn()
res=re.sub("funny","fool","You are so funny")
print(res)
res=re.subn("funny","fool","You are so funny")
print(res)


# 分割
# splite()
res=re.split("\.","Mr.Smith")
print(res)



print("#"*50)
# 择一匹配符号 a|b
pattern="I|You|She"
res=re.compile(pattern,flags=re.IGNORECASE).match("i love you")
print_match_res(res)
res=re.compile(pattern,flags=re.I).search("who love you")
print_match_res(res)

# 匹配任意单个字符 .
pattern="w{3,}\..+\.com"
res=re.match(pattern,"wwww.google.com/index.html",re.I)
print_match_res(res)

# 字符集 [abc] [a-z0-9]
pattern="[A-Za-z0-9_]*\."
res=re.match(pattern,"Python3.?")
print_match_res(res)

# 特殊字符 \d \w \s \b \\
# 重复 + ? * {N,} {N,M}
# 分组 (...)
pattern="\w+@(\w{1,10}\.)*([a-z]*)"
res=re.match(pattern,"li@gmail.com")
print_match_res(res)
res=re.match(pattern,"li@qq.vip.org")
print_match_res(res)
print(res.group(0),res.group(1),res.group(2),sep="\t")
print(res.groups())

# 匹配字符串的起始和结尾,单词边界  ^a z$ \A \Z \b \B
pattern=r"^the"
# pattern=r"\Athe"
res=re.search(pattern,"The end of the world")
print_match_res(res)
res=re.search(pattern,"they smile")
print_match_res(res)

pattern=r"cry$"
# pattern=r"cry\Z"
res=re.search(pattern,"they cry")
print_match_res(res)

res=re.search(r"\bthe","bit the dog")
print_match_res(res)
res=re.search(r"\Bhe","bit the dog")
print_match_res(res)



  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Python,可以使用正则表达式模块re来进行正则表达式的操作。首先需要导入re模块,使用import re。然后可以使用re模块提供的各种函数进行正则表达式的匹配、查找、提取和替换等操作。其,常用的函数包括findall、search、match和sub等。 - findall函数可以用于查找字符串所有匹配正则表达式的子串,并以列表的形式返回所有匹配的结果。 - search函数可以在整个字符串搜索匹配正则表达式的第一个子串,并返回一个匹配对象。如果找到匹配的子串,则可以通过匹配对象的group方法得到匹配的结果。 - match函数可以在字符串的开头匹配正则表达式,并返回一个匹配对象。如果在开头找到匹配的子串,则可以通过匹配对象的group方法得到匹配的结果。 - sub函数可以用于替换字符串匹配正则表达式的子串。 使用正则表达式时,还可以使用一些基础的符号来表示规则,比如“.*”表示匹配任意字符任意次数,“.*?”表示非贪婪匹配。这些符号可以用于更灵活地匹配和提取字符串的信息。 总结起来,Python正则表达式用法包括导入re模块、使用findall、search、match和sub等函数来进行匹配、查找、提取和替换等操作,并可以使用一些基础的符号来表示匹配规则。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [【Python正则表达式的使用](https://blog.csdn.net/heiren_a/article/details/123174223)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值