1. 程式人生 > >Python學習手冊之正則表示式和元字元

Python學習手冊之正則表示式和元字元

 在上一篇文章中,我們介紹了 Python 的資料封裝、類方法、靜態方法和屬性函式,現在我們介紹 Python 的正則表示式和元字元。檢視上一篇文章請點選:https://www.cnblogs.com/dustman/p/10019973.html

正則表示式
正則表示式是一種強大的字串操作工具。它是一種領域特定語言 (DSL),不管是 Python 還是在大多數現代程式語言中都是作為庫存在。
它們主要面向兩種任務:
- 驗證字串是否與模式匹配 (例如,字串具有電子郵件地址的格式)。
- 在字串中執行替換(例如將所有大寫字母改成小寫字母)。

特定於領域的語言是高度專業化的迷你程式語言。
正則表示式是一個例子,SQL(用於資料庫操作)是另一個例子。
私有領域特定語言通常用於特定的工業目的。

Python 的正則表示式可以使用 re 模組訪問,re 模組是標準庫的一部分。
當你定義一個正則表示式,可以使用 re.match 函式用於確定是否匹配字串的開始部分。如果匹配則 match 函式返回表示匹配的物件,如果不匹配則返回 None。
為了避免在處理正則表示式時出現混淆,我們將 r 新增到字串字首。該字串不需要轉義任何東西,使得正則表示式的使用變得更容易。

from re import match

msg = r"super"

if match(msg,"superman!"):
 print("You are True")

else:
 print("
Occur an error! Foolish...")

執行結果:

>>>
You are True
>>>

上面的例子檢查模式 super 是否匹配字串,如果匹配,則列印 You are True。

這裡的模式是一種簡單的單詞,但是有些字串,在正則表示式中使用它們時會有特殊的意義。

匹配模式的其他函式有 re.matchre.findall
re.match 在字串中找到匹配。
re.findall 返回一個包含匹配的列表。

import re

string = "Hello python!Hello python!Hello python!
" pattern = r".python."

print(re.match(pattern,string)) print(re.findall(pattern,string))

執行結果:

>>>
None
[' python!', ' python!', ' python!']
>>>

從上面的示例中,我們可以得出:
match() 函式是從內容的第一個字元開始匹配,如果匹配不到,就得到None
findall() 函式從全部內容匹配,如果有多個,找出所有匹配的

函式 re.finditer 執行與 re.findall 相同的操作,但它返回一個迭代器,而不是一個列表。

正則表示式的 search 函式返回一個物件,包含幾個更詳細的資訊。
此方法包括返回字串匹配的值,返回第一次匹配的開始和結束位置,以及以元組形式返回第一個匹配的開始和結束位置的 span 函式。

import re
string
= "Hello python!Hello python!Hello python!" pattern = r".python." match = re.search(pattern,string)
if match: print(match.group()) print(match.start()) print(match.end()) print(match.span())

執行結果:

>>>
python!
5
13
(5, 13)
>>>

查詢和替換
sub 是正則表示式裡非常重要的函式。表示式:

re.sub(pattern, repl, string, count=0, flags=0)

pattern:表示正則表示式中的模式字串;
repl:被替換的字串(既可以是字串,也可以是函式);
string:要被處理的,要被替換的字串;
count:匹配的次數, 預設是全部替換
flags:具體用處不詳

import re
string
= "Hello python!Hello python!Hello python!" pattern = r"python" newstr = re.sub(pattern,"Java",string) print(newstr)

執行結果:

>>>
Hello Java!Hello Java!Hello Java!
>>>

元字元
元字元使正則表示式比普通字串方法更強大。它們允許您建立正則表示式來表示諸如一個或多個數字的匹配。
如果要建立與元字元 (如 $) 匹配的正則表示式,元字元的存在就會產生問題。您可以通過在元字元前面新增反斜槓來轉義元字元。
但是這可能會導致問題,因為反斜槓在普通 Python 字串中也有轉義函式。這可能意味著可能將三個或四個反斜槓排成一行來執行所有轉義操作。

為了避免這種情況,您可以使用一個原始字串,它是一個普通字串,前面有一個 "r" 字首。

元字元點,用來表示匹配除了換行外的任何字元。

import re
string1
= "Hello python!Hello python!Hello python!" string2 = "pythan,1234587pythoi" string3 = r"hello" pattern = r"pyth.n" match1 = re.search(pattern,string1) match2 = re.search(pattern,string2) match3 = re.search(pattern,string3)
if match1: print(match1.group()) print("match 1") if match2: print(match1.group()) print("match 2") if match3: print(match3.group()) print("match 3")

執行結果:

>>>
python
match 1
python
match 2
>>>

^ 表示匹配開始,$ 表示匹配結束。

import re
string1
="python" string2="pythan,1234587pythoi" string3="hello" pattern=r"^pyth.n$" match1 = re.search(pattern,string1) match2 = re.search(pattern,string2) match3 = re.search(pattern,string3)
if match1: print(match1.group()) print("match 1") if match2: print(match1.group()) print("match 2") if match3: print(match3.group())
print("match 3")

執行結果:

>>>
python
match 1
>>>
匹配模式 "^pyth.n$" 意味著字串應該以 pyth 開頭,然後是一個除換行符以外的任何字元,並以 n 結尾。

 

 

 

“We go through life. We shed our skins. We become ourselves.”

 “我們經歷人生,我們脫胎換骨。我們稱為自己” -- 帕蒂·史密斯