Python进阶知识:整理6 -> 正则表达式

1 基础匹配用法

python 复制代码
# 演示Python中正则表达式re模块的3个基础匹配方法
import re
# 1. match()方法  从头匹配
string = "hello world"
result = re.match("hello", string)   # 如果头部没有匹配成功就直接失败了,后面就不会继续匹配了
print(result)
print(result.group())
print(result.span())
print("--------------------")



# 2. search()方法  搜索匹配
s1 = "python11888999python"
result = re.search("python", s1)   # 搜索整个字符串,找到第一个匹配的,并返回结果
print(result)
print(result.group())
print(result.span())
print("----------------------")




# 3. findall()方法  搜索匹配所有,返回一个列表
s2 = "python11888999python"
result = re.findall("python", s2)
print(result)
print("--------------------")

2 元字符匹配

python 复制代码
"""
    字符            功能
     .             匹配任意1个字符(除了\n), \. 匹配点本身
     [...]         匹配[]中列举的字符, 字符集合, [0-9a-zA-Z_], [a-z], [0-9], [...]
     \d            匹配数字, [0-9]的简写
     \D            匹配非数字, [^0-9]的简写
     \w            匹配字母或数字或下划线, [a-zA-Z0-9_]的简写
     \W            匹配非(字母或数字或下划线), [^a-zA-Z0-9_]的简写
     \s            匹配空白, [ \t\n\x0b\r]的简写, 即空格, tab键
     \S            匹配非空白, [^ \t\n\x0b\r]的简写
     [^...]        匹配除了[]中列举的字符之外的字符, 反向字符集合, [^0-9], [^a-zA-Z_]
"""

import re
s = "itheima @@ Python2 !!999 # It"
result = re.findall(r"\d", s)    # 加上 r 可以表示字符串中的转义字符无效
print(result)
print("---------------------")

result2 = re.findall(r"\W", s)  # 非单词字符
print(result2)
print("---------------------")

# 匹配所有的英文字母
result3 = re.findall(r"[a-zA-Z]", s)
print(result3)


"""
    数量匹配:
     *             匹配*号 前的字符 0次或无数次
     +             匹配+号 前的字符 1次或无数次
     ?             匹配?号 前的字符 0次或1次
     {m}           匹配{m} 前的字符 出现m次
     {m,}          匹配{m} 前的字符 出现最少m次
     {m,n}         匹配{m,n} 前的字符 出现 m到n次
"""

"""
    边界匹配:
     ^             匹配一行字符串的开头
     $             匹配一行字符串的结束
     \b            匹配一个单词的边界
     \B            匹配非单词边界
"""

"""
    分组匹配:
      |            匹配左右任意一个表达式
     (...)         分组, 将 ()中的内容作为一组, (abc.efs), (a|b|c)   
"""




# 案例:  (注意: 正则表达式中千万不要随意价格空格)
# 1. 匹配账号: 只能由字母数字组成,长度限制6-10位
r = '^[a-zA-Z0-9]{6,10}$'
s = '1232dfgf'
result = re.findall(r, s)
print(result)
print('---------------------')



# 2. 匹配QQ号, 要求纯数字, 长度5-11,第一位不为0
r = '^[1-9][0-9]{4,10}$'    # {4,10} 开头已经占了一位了
s_qq = '329809378'
result_qq = re.findall(r, s_qq)
print(result_qq)
print('---------------------')




# 3. 匹配邮箱地址,只允许请qq 、163、gmail这三种邮箱地址
# abc.ghs.edu@qq.com
# abc.ghs.edu@qq.jsx.ss.com
# abc.@163.com
r = '^[\w-]+(\.[\w-]+)*@(qq|163|gmail)(\.[\w-]+)+$'
s = 'abc.ghs.edu@qq.com'
result = re.findall(r, s)   # findall  会返回正则中的分组 ()   -> [('.edu', 'qq', '.com')]
# 所以需要整体再放入一个组中
r = '(^[\w-]+(\.[\w-]+)*@(qq|163|gmail)(\.[\w-]+)+$)'
result_all = re.findall(r, s)
result_match = re.match(r, s)
print(result_match)
print(result_all)
print("----------------------")
相关推荐
博观而约取1 小时前
Django ORM 1. 创建模型(Model)
数据库·python·django
精灵vector2 小时前
构建专家级SQL Agent交互
python·aigc·ai编程
Zonda要好好学习2 小时前
Python入门Day2
开发语言·python
Vertira2 小时前
pdf 合并 python实现(已解决)
前端·python·pdf
太凉3 小时前
Python之 sorted() 函数的基本语法
python
项目題供诗3 小时前
黑马python(二十四)
开发语言·python
晓13133 小时前
OpenCV篇——项目(二)OCR文档扫描
人工智能·python·opencv·pycharm·ocr
是小王同学啊~3 小时前
(LangChain)RAG系统链路向量检索器之Retrievers(五)
python·算法·langchain
AIGC包拥它3 小时前
提示技术系列——链式提示
人工智能·python·langchain·prompt
孟陬3 小时前
Python matplotlib 如何**同时**展示正文和 emoji
python