• 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
  • 相关阅读:
    Nacos集群搭建
    (09_22)【有奖体验】轻点鼠标,让古籍数字化“重生_
    ant的echo任务
    性能监测工具-node-export
    Linux实用操作-----快捷键的使用(收藏系列)
    【SA8295P 源码分析】108 - QNX AIS qcarcam_test 如何实现获取摄像头buffer 数据,追根溯源来看看它是从何而来?
    Linux Control Cgroups
    RK3566恢复显示屏异常显示的方法
    深入Python网络编程:基础、工具和实践
    R语言使用cph函数和rcs函数构建限制性立方样条cox回归模型、使用anova函数进行方差分析通过p值确认指定连续变量和风险值HR之间是否存在非线性关系
  • 原文地址:https://blog.csdn.net/weixin_36445197/article/details/133994764