• python正则表达式的使用


    Python中的正则表达式模块re提供了许多常用的方法和功能,用于匹配和处理文本。以下是一些常用的方法和示例:

    1. re.match(pattern, string):从字符串的开始位置匹配指定的正则表达式模式,如果匹配成功,返回一个匹配对象,否则返回None。

    示例:

    import re
    
    string = "Hello, World!"
    match = re.match(r'Hello', string)
    if match:
        print("Matched!") # Matched!
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    1. re.search(pattern, string):在整个字符串中搜索指定的正则表达式模式,如果匹配成功,返回第一个匹配对象,否则返回None。

    示例:

    import re
    
    string = "Hello, World! Welcome to Python."
    match = re.search(r'Python', string)
    if match:
        print("Found!") # Found!
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    1. re.findall(pattern, string):在字符串中搜索所有匹配正则表达式模式的子串,返回一个包含所有匹配结果的列表。

    示例:

    import re
    
    string = "apple,banana,orange,grape"
    fruits = re.findall(r'\b\w+\b', string)
    print(fruits) # ['apple', 'banana', 'orange', 'grape']
    
    • 1
    • 2
    • 3
    • 4
    • 5
    1. re.finditer(pattern, string):在字符串中搜索所有匹配正则表达式模式的子串,返回一个包含所有匹配对象的迭代器。

    示例:

    import re
    
    string = "Hello, World! Welcome to Python."
    for match in re.finditer(r'\w+', string):
        print(match.group()) # Hello, World, Welcome, Python
    
    • 1
    • 2
    • 3
    • 4
    • 5
    1. re.sub(pattern, repl, string):在字符串中使用正则表达式模式匹配并替换子串,返回替换后的字符串。

    示例:

    import re
    
    string = "Hello, 123 World!"
    new_string = re.sub(r'\d+', 'XYZ', string)
    print(new_string) # Hello, XYZ World!
    
    • 1
    • 2
    • 3
    • 4
    • 5
    1. re.subn(pattern, repl, string):匹配字符并替换,返回元组类型(替换后的字符串,被替换次数)。

    示例:

    import re
    
    string = "Hello, World!"
    new_string, count = re.subn(r'o', 'x', string)
    print(new_string) # Hellx, Wxrld!
    print(count) # 2
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    1. re.split(pattern, string):根据正则表达式模式将字符串拆分为子串,并返回一个列表。

    示例:

    import re
    
    string = "apple,banana,orange,grape"
    fruits = re.split(r',', string)
    print(fruits) # ['apple', 'banana', 'orange', 'grape']
    
    • 1
    • 2
    • 3
    • 4
    • 5
    1. re.finditer(pattern, string):在字符串中搜索所有匹配正则表达式模式的子串,返回一个包含所有匹配对象的迭代器。

    示例:

    import re
    
    string = "Hello, World! Welcome to Python."
    for match in re.finditer(r'\w+', string):
        print(match.group()) # Hello, World, Welcome, Python
    
    • 1
    • 2
    • 3
    • 4
    • 5
    1. re.compile(pattern):编译正则表达式模式,返回一个可重复使用的正则表达式对象。

    示例:

    import re
    
    pattern = r'\d+'
    regex_object = re.compile(pattern)
    match = regex_object.match('12345')
    if match:
        print("Matched digit:", match.group()) # Matched digit: 12345
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    1. re.escape(string):在字符串中转义特殊字符,返回转义后的字符串。

    示例:

    import re
    
    string = 'Hello, *.py'
    escaped_string = re.escape(string)
    print(escaped_string) # Hello\, \*.py
    
    • 1
    • 2
    • 3
    • 4
    • 5
    1. re.purge():清除正则表达式模块中的所有编译过的模式,释放内存。

    示例:

    import re
    re.purge()
    
    • 1
    • 2
  • 相关阅读:
    后端研发工程师面经——数据结构
    Mybatis传入参数字符串分割成数组作为条件遍历不用in不用in
    一份超预期的期中成绩,拨开百果园“高价值迷雾”
    实现Ant Design Vue的modal可拖拽
    最新WooCommerce教程指南-如何搭建B2C外贸独立站
    AI办公自动化:用kimi批量提取音频中的标题并重命名
    B44 - 基于stm32蓝牙智能语音识别分类播报垃圾桶
    Abnova LiquidCell-负富集细胞分离和回收系统
    MySQL如何进行增量备份与恢复?
    javaEE进阶 - Spring 更简单的读取和存储对象 - 细节狂魔
  • 原文地址:https://blog.csdn.net/weixin_36445197/article/details/133994764