Python正則表達式使用技巧

Python正則表達式使用技巧

使用方法都寫在程序裏面的註釋中,請盡情享用,如果您覺得不錯可以點個贊哦🙂
代碼如下:

"""常用的匹配規則:
模式:              描述:
\w                匹配字母,數字及下劃線
\W                匹配不是字母,數字及下劃線的字符
\s                匹配任意空白字符
\S                匹配任意非空字符
\d                匹配任意數字
\D                匹配任意非數字的字符
\z                匹配字符串結尾,如果存在換行,同時還會匹配到換行符
\Z                匹配字符串結尾,如果存在換行,只匹配到換行前的結束字符串
\G                匹配最後完成匹配的位置
\n                匹配一個換行符
\t                匹配一個製表符
^                 匹配一行字符串的開頭
$                 匹配一行字符串的結尾
.                 匹配任意字符,除了換行符
[...]             用來表示一組字符,單獨列出,比如[abc]: 匹配 A、b或c 的字符
[^...]            匹配不在[]中的字符,比如[abc]: 匹配除了 A、b或c 之外的字符
*                 匹配0個或多個前面的表達式
+                 匹配1個或多個前面的表達式
?                匹配0個或1個前面的表達式,非貪婪方式
{n}               精確匹配n個前面的表達式
{n, m}            匹配n到m次前面的表達式,貪婪方式
a|b               匹配a或b
()                匹配括號內的表達式,也表示一個組

<<修飾符>>
模式:              描述:
re.S              使.匹配包括換行在內的所有字符
re.I              使匹配對大小寫不敏感
re.M              多行匹配,影響^和$
re.L              做本地化識別(locale-aware)匹配
re.U              根據Unicode字符集解析字符,則個標誌影響\w,\W,\b,\B
re.X              該標誌通過給予你更靈活的格式以便你將正則表達式寫得更易於理解
"""
# -*- coding:utf-8 -*-
import re

__author__ = 'Evan'
example = """Just as you need air to breathe,
you need opportunity to succeed. It takes more than just breathing in the fresh air of opportunity,
however. You must make use of that opportunity. That's not up to the opportunity. That's up to you.
It doesn't matter what "floor" the opportunity is on. What matters is what you do with it.
"""


def parse():
    # 匹配方式
    print(re.match('Just as you need air to breathe', example).group())  # 起始位置匹配
    print(re.search("That's up to you", example).group())  # 任意位置匹配
    print(re.findall("is", example))  # 匹配所有指定內容,返回一個列表

    # 分組匹配
    result = re.search(r'You (.+?)\..+?What (.+?)\.', example, re.S)
    if result:
        print(result.group())  # 返回所有的匹配結果
        print(result.group(1))  # 返回第一個圓括號的匹配結果
        print(result.group(2))  # 返回第二個圓括號的匹配結果
        print(result.groups())  # 返回一個列表,包含所有圓括號的結果

    # 輸出匹配的範圍
    print(re.match('Just as you need air to breathe', example).span())  # 返回一個元組

    # 替換字符串
    print(re.sub('what', 'happy', example))  # 把所有的'what'替換成'happy'

    # 編譯正則表達式,可重複使用
    pattern = re.compile('succeed')
    print(re.findall(pattern, example))

    # 匹配中文
    text = 'abc我愛你def中國'
    pattern = '[\u4E00-\u9FA5]+'  # 匹配所有的中文
    print(re.findall(pattern, text))

    # 只匹配字符串中的中文,字母,數字
    print(re.match('[\u4e00-\u9fa5a-zA-Z0-9]+', text).group())


if __name__ == '__main__':
    parse()

執行結果:

Just as you need air to breathe
That's up to you
['is', 'is']
You must make use of that opportunity. That's not up to the opportunity. That's up to you.
It doesn't matter what "floor" the opportunity is on. What matters is what you do with it.
must make use of that opportunity
matters is what you do with it
('must make use of that opportunity', 'matters is what you do with it')
(0, 31)
Just as you need air to breathe,
you need opportunity to succeed. It takes more than just breathing in the fresh air of opportunity,
however. You must make use of that opportunity. That's not up to the opportunity. That's up to you.
It doesn't matter happy "floor" the opportunity is on. What matters is happy you do with it.

['succeed']
['我愛你', '中國']
abc我愛你def中國
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章