• 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
  • 相关阅读:
    Puppeteer监听网络请求、爬取网页图片(二)
    智能井盖:把好城市地下“安全门”
    MCE丨重组蛋白常见的融合标签
    在PyCharm中使用Jupyter Notebooks实现高效开发
    QtSqlDatabase-QCryptographicHash数据库-加密
    Python 变量的定义和数据类型的转换
    ZYNQ7020--AMP下裸机程序开发 <2>
    【NLP教程】用python调用百度AI开放平台进行情感倾向分析
    常见设计模式之单例模式
    RabbitMQ深入 —— 持久化和发布确认
  • 原文地址:https://blog.csdn.net/weixin_36445197/article/details/133994764