應用程式設計/正則表示式
在程式設計中,正則表示式指的是一系列字元,可以用來指定搜尋功能。它們允許以健壯且強大的方式搜尋大量資料,而無需建立冗餘或重複的程式碼行。正則表示式(簡稱 regex)就像程式碼中的 Ctrl+F,允許我們輕鬆地檢索資料模式,例如電子郵件地址、域名、電話號碼;實際上,任何我們可以歸因於模式的資料。
正則表示式字元由特殊元字元和用於指定要匹配字串元素的某些標準字元的組合構成。
元字元是一個對計算機程式(如 Shell 直譯器或正則表示式 (regex) 引擎)具有特殊意義的字元。
字元[1]
- []
一組字元
import re
txt = "The rain in Spain"
#Find all lower case characters alphabetically between "a" and "m":
x = re.findall("[a-m]", txt)
print(x)
輸出
['h', 'e', 'a', 'i', 'i', 'a', 'i']
- \
表示特殊序列(也可以用於轉義特殊字元)
import re
txt = "That will be 59 dollars"
#Find all digit characters:
x = re.findall("\d", txt)
print(x)
輸出
['5', '9']
- .
任何字元(換行符除外)
import re
txt = "hello world"
#Search for a sequence that starts with "he", followed by two (any) characters, and an "o":
x = re.findall("he..o", txt)
print(x)
輸出
['hello']
- ^
以…開頭
import re
txt = "hello world"
#Check if the string starts with 'hello':
x = re.findall("^hello", txt)
if x:
print("Yes, the string starts with 'hello'")
else:
print("No match")
輸出
Yes, the string starts with 'hello'
- $
以…結尾
import re
txt = "hello world"
#Check if the string ends with 'world':
x = re.findall("world$", txt)
if x:
print("Yes, the string ends with 'world'")
else:
print("No match")
輸出
Yes, the string ends with 'world'
- *
零個或多個出現
import re
txt = "The rain in Spain falls mainly in the plain!"
#Check if the string contains "ai" followed by 0 or more "x" characters:
x = re.findall("aix*", txt)
print(x)
if x:
print("Yes, there is at least one match!")
else:
print("No match")
輸出
['ai', 'ai', 'ai', 'ai'] Yes, there is at least one match!
- +
一個或多個出現
import re
txt = "The rain in Spain falls mainly in the plain!"
#Check if the string contains "ai" followed by 1 or more "x" characters:
x = re.findall("aix+", txt)
print(x)
if x:
print("Yes, there is at least one match!")
else:
print("No match")
輸出
[] No match
- {}
指定數量的出現
import re
txt = "The rain in Spain falls mainly in the plain!"
#Check if the string contains "a" followed by exactly two "l" characters:
x = re.findall("al{2}", txt)
print(x)
if x:
print("Yes, there is at least one match!")
else:
print("No match")
輸出
['all'] Yes, there is at least one match!
- |
兩者之一
import re
txt = "The rain in Spain falls mainly in the plain!"
#Check if the string contains either "falls" or "stays":
x = re.findall("falls|stays", txt)
print(x)
if x:
print("Yes, there is at least one match!")
else:
print("No match")
輸出
['falls'] Yes, there is at least one match!
其他示例[2]
在某些環境中,某些其他字元可能具有特殊含義。
- 在某些 Unix Shell 中,分號 (";") 是語句分隔符。
- 在 XML 和 HTML 中,與號 ("&") 引入 HTML 實體。它在 MS-DOS/Windows 命令提示符中也具有特殊含義。
- 在某些 Unix Shell 和 MS-DOS/Windows 命令提示符中,小於號和大於號 ("<" 和 ">") 用於重定向,重音符/反引號 ("`") 用於命令替換。
- 在許多程式語言中,字串使用引號 (" 或 ') 定界。在某些情況下,跳脫字元(和其他方法)用於避免定界符衝突,例如 "他說,"你好""。
- 在 printf 格式字串中,百分號 ("%") 用於引入格式說明符,必須轉義為 "%%" 才能按字面意義解釋。在 SQL 中,百分號用作萬用字元。
- 在 SQL 中,下劃線 ("_") 用於匹配任何單個字元。
貪婪是一種演算法正規化,它逐段構建解決方案,始終選擇提供最明顯和最直接好處的下一段。因此,選擇區域性最優也導致全域性解決方案的問題最適合貪婪。 [3]
以下是使用貪婪方法的原因
- 貪婪方法有一些權衡,這可能使其適合最佳化。
- 一個突出的原因是立即獲得最可行的解決方案。在活動選擇問題中(如下所述),如果可以在完成當前活動之前執行更多活動,則可以在同一時間內執行這些活動。
- 另一個原因是根據條件遞迴地劃分問題,而無需組合所有解決方案。
- 在活動選擇問題中,"遞迴劃分"步驟是透過僅掃描專案列表一次並考慮某些活動來實現的。 [4]
貪婪演算法有一些優點和缺點
- 提出貪婪演算法(甚至多個貪婪演算法)非常容易。
- 分析貪婪演算法的執行時間通常比其他技術(如分治法)容易得多。對於分治法,目前尚不清楚該技術是快還是慢。這是因為在每次遞迴級別上,大小都會變小,子問題數量會增加。
- 困難的部分是,對於貪婪演算法,您必須更努力地理解正確性問題。即使使用正確的演算法,也很難證明為什麼它是正確的。證明貪婪演算法是正確的更像是一門藝術,而不是一門科學。它需要大量的創造力。 [5]
1) 編寫一個程式,使用正則表示式解析包含單個電子郵件地址的文字檔案,並將它們排序到字典中,其中鍵是電子郵件地址的控制代碼,值是域。
示例:包含'johndoe@aol.com' 和 'janedoh@yahoo.com' 的檔案將導致["johndoe": "aol", "janedoe": "yahoo"]。
2) 編寫一個程式,使用正則表示式查詢並替換字串中所有母音的出現,並將它們替換為句點。僅在 'y' 是單詞中唯一的母音時才替換 'y'。
示例:"The quick brown fox jumped over the lazy dog" 將導致 "Th. q..ck br.wn f.x j.mp.d .v.r th. l.zy d.g"
3) 編寫一個程式,使用正則表示式將 dd-mm-yyyy 格式的日期轉換為 yy-mm-dd 格式。
示例:06-04-1992 將導致 92-06-04。
4) 編寫一個程式,使用正則表示式分解 URL 並將資訊排序到字典中。鍵應該是主要域,而值應該是子頁面的列表。
示例:"https://www.wikipedia.org/wiki/Computer_programming" 將導致["wikipedia": ['wiki', 'Computer_programming']]
近似字串匹配演算法 - (也稱為模糊字串搜尋)搜尋輸入字串的子字串。 [6]
跳脫字元 - 用於繞過可能為元字元的符號並按字面意義表達符號的字元。
精確字串匹配演算法 - 在大型字串(文字或序列)中查詢一個、多個或所有已定義字串(模式)的出現,以使每個匹配都完美。 [6]
貪婪演算法 - 一種演算法正規化,它逐段構建解決方案,始終選擇提供最明顯和最直接好處的下一段。 [3]
Kleene 星號 - 提供與字串集串聯相關的結果的程式設計資源。使用 Kleene 星號,開發人員和其他人員可以評估如何根據輸入過濾給定結果。 [7]
元字元 - 對計算機程式(如 Shell 直譯器或正則表示式 (regex) 引擎)具有特殊意義的字元。 [8]
么半群 - 一個集合,它配備了結合的二元運算和單位元。[9]
模式 - 一個正則表示式,用來匹配或捕獲文字。[10]
量詞 - 修飾符,它跟隨另一個正則表示式標記,列舉預期出現的次數。[10]
正則表示式處理器 - 將以上語法中的正則表示式轉換為內部表示的處理器,可以執行並與表示要搜尋文字的字串進行匹配。[10]
正則表示式 - 正則表示式(簡稱 regex 或 regexp)是一種描述搜尋模式的特殊文字字串。[11]
萬用字元 - 代表任何內容的通用字元。[10]
參考文獻
[edit | edit source]- ↑ https://w3schools.tw/python/gloss_python_regex_metacharacters.asp
- ↑ 元字元
- ↑ a b https://www.geeksforgeeks.org/greedy-algorithms/
- ↑ https://www.guru99.com/greedy-algorithm.html
- ↑ https://www.hackerearth.com/practice/algorithms/greedy/basics-of-greedy-algorithms/tutorial/
- ↑ a b https://www.geeksforgeeks.org/applications-of-string-matching-algorithms/
- ↑ https://www.techopedia.com/definition/20267/kleene-star
- ↑ https://en.wikipedia.org/wiki/Metacharacter
- ↑ https://en.wikipedia.org/wiki/Monoid
- ↑ a b c d https://en.wikipedia.org/wiki/Regular_expression
- ↑ https://www.regular-expressions.info/