一、正則表達式或符號
正則表達式作為一種表示文本模式的強大工具,包含了多種符號用於匹配文本模式。其中最基礎的符號之一就是 “|” 符號,表示或(OR)的關係。
舉個例子,我們可以使用正則表達式 “hello|hi|hey” 來匹配三個單詞中的任何一個,即匹配文本中出現的 “hello”,“hi”,“hey” 中的任何一個單詞。
pattern = re.compile(r"hello|hi|hey") matches = pattern.findall("Hello, hi, Hey!") print(matches) # 輸出 ['Hello', 'hi', 'Hey']
二、正則表達式或規則
正則表達式中的 “|” 符號有一個很實用的功能,即能夠匹配多種可能性的模式。
舉個例子,如果我們想要匹配一個單詞是由 “A” 或 “a” 開頭的字符串,可以使用正則表達式 “[Aa]\w+”。
“[Aa]” 表示匹配一個以 A 或 a 開頭的字符,“\w+” 則表示匹配至少一個“單詞字符”(字母、數字或下劃線)。
pattern = re.compile(r"[Aa]\w+") matches = pattern.findall("a12 Apple and A OrAnge!") print(matches) # 輸出 ['a12', 'Apple', 'And', 'Ange']
三、正則表達式或者怎麼表示
正則表達式中的“|”是指或者,可以匹配多個待選字符串,這種情況下,它通常和括號“()”配合使用來表示多個可能結果的組合。
舉個例子,如果我們想把一個全名分為三個組:位置,名,姓,可以使用正則表達式“^(Mr|Mrs|Ms)\. (\w+) (\w+)$”。
該正則表達式含義是:以“Mr”,“Mrs” 或者 “Ms” 開頭,後接一個以空格分隔的名字(\w+),再後接一個以空格分隔的姓氏(\w+)。
pattern = re.compile(r"^(Mr|Mrs|Ms)\. (\w+) (\w+)$") matches = pattern.search("Mr. John Smith") print(matches.groups()) # 輸出 ('Mr', 'John', 'Smith')
四、正則表達式或規則
正則表達式中的“|”有時候還可以用於將多個模式合併為一個新模式。例如,“cat|dog” 表示匹配“cat” 或 “dog”。
舉個例子,如果我們想匹配一個由數字或字母組成的字符串,可以使用正則表達式 “\d+|[a-zA-Z]+”。
“\d+” 表示匹配一個或多個數字,“[a-zA-Z]+” 表示匹配一個或多個字母。
pattern = re.compile(r"\d+|[a-zA-Z]+") matches = pattern.findall("abc123def456") print(matches) # 輸出 ['abc', '123', 'def', '456']
五、正則表達式或關係
正則表達式中的 “|” 符號可以用於表示或關係,而且這種或關係可以串聯多個子模式。例如,“apple|orange|banana” 可以匹配文本中包含 “apple”、“orange” 或者 “banana” 中任意一個單詞的模式。
該正則表達式實現了默認大小寫匹配。
pattern = re.compile(r"apple|orange|banana", re.IGNORECASE) matches = pattern.findall("I ate an APPLE, an Orange, and a banana.") print(matches) # 輸出 ['APPLE', 'Orange', 'banana']
六、正則表達式或者怎麼寫
正則表達式的 “|” 可以用於選擇任意多個子模式的匹配。如果需要匹配一個模式序列中的任意一個子模式,可以使用圓括號進行分組。
例如,“(foo|bar|baz)” 可以匹配 “foo”,“bar”,“baz” 中任意一個單詞。
pattern = re.compile(r"(foo|bar|baz)") matches = pattern.findall("foo, bar and baz.") print(matches) # 輸出 ['foo', 'bar', 'baz']
七、正則表達式或運算符
正則表達式中的或運算符使用“|”進行表示,可以用於匹配兩個或多個模式中的任意一個。例如,“(car|train)” 可以匹配 “car” 或 “train” 中任意一個單詞。
pattern = re.compile(r"(car|train)") matches = pattern.findall("I prefer to take the train or car to work.") print(matches) # 輸出 ['train', 'car']
八、正則表達式或的用法
正則表達式中,“|” 符號可以將兩個或多個單詞模式進行或操作,並匹配作為單一模式的相應文本。例如,“dog|cat” 可以匹配文本中包含“dog”或“cat”的單詞。
pattern = re.compile(r"dog|cat") matches = pattern.findall("I have a cat and a dog.") print(matches) # 輸出 ['cat', 'dog']
九、代碼部分
import re # 正則表達式符號 pattern1 = re.compile(r"hello|hi|hey") matches1 = pattern1.findall("Hello, hi, Hey!") print(matches1) # 輸出 ['Hello', 'hi', 'Hey'] # 正則表達式或規則 pattern2 = re.compile(r"[Aa]\w+") matches2 = pattern2.findall("a12 Apple and A OrAnge!") print(matches2) # 輸出 ['a12', 'Apple', 'And', 'Ange'] # 正則表達式或者怎麼表示 pattern3 = re.compile(r"^(Mr|Mrs|Ms)\. (\w+) (\w+)$") matches3 = pattern3.search("Mr. John Smith") print(matches3.groups()) # 輸出 ('Mr', 'John', 'Smith') # 正則表達式或規則 pattern4 = re.compile(r"\d+|[a-zA-Z]+") matches4 = pattern4.findall("abc123def456") print(matches4) # 輸出 ['abc', '123', 'def', '456'] # 正則表達式或關係 pattern5 = re.compile(r"apple|orange|banana", re.IGNORECASE) matches5 = pattern5.findall("I ate an APPLE, an Orange, and a banana.") print(matches5) # 輸出 ['APPLE', 'Orange', 'banana'] # 正則表達式或者怎麼寫 pattern6 = re.compile(r"(foo|bar|baz)") matches6 = pattern6.findall("foo, bar and baz.") print(matches6) # 輸出 ['foo', 'bar', 'baz'] # 正則表達式或運算符 pattern7 = re.compile(r"(car|train)") matches7 = pattern7.findall("I prefer to take the train or car to work.") print(matches7) # 輸出 ['train', 'car'] # 正則表達式或的用法 pattern8 = re.compile(r"dog|cat") matches8 = pattern8.findall("I have a cat and a dog.") print(matches8) # 輸出 ['cat', 'dog']
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/159632.html