如何使用正則表示式在 Python 中匹配非空白字元?
在 Python 中,**非空白字元**是指任何不是空格、製表符或換行符的字元。這些字元對於 Python 程式碼的格式和可讀性非常重要。
假設我們有一個包含空白字元和非空白字元的字串:我們可以使用 isspace() 方法檢查字串中每個字元是否為空白字元。
在此程式碼中,我們遍歷 my_string 變數中的每個字元,並使用 isspace() 方法確定該字元是否為空白字元。如果該字元為空白字元,則列印“空白字元”,如果是非空白字元,則列印“非空白字元”。
示例
my_string = "Hello, world!" for char in my_string: if char.isspace(): print("Whitespace character") else: print("Non-whitespace character")
輸出
Non-whitespace character Non-whitespace character Non-whitespace character Non-whitespace character Non-whitespace character Non-whitespace character Whitespace character Whitespace character Whitespace character Whitespace character Whitespace character Non-whitespace character Non-whitespace character Non-whitespace character Non-whitespace character Non-whitespace character Non-whitespace character
在此輸出中,我們可以看到 isspace() 方法識別了空白字元(空格),而非空白字元(字母、逗號和感嘆號)則沒有被識別。
使用字元類
在 Python 正則表示式中匹配非空白字元的一種方法是使用字元類。以下是一個示例
示例
在此示例中,我們定義了一個正則表示式模式,該模式使用 \S
元字元匹配單個非空白字元。然後,我們使用 re.findall()
函式查詢測試字串中正則表示式的所有匹配項並列印匹配項。
import re # Define a regular expression pattern to match a non-whitespace character using character classes pattern = r"\S" # Define a test string test_string = "Lorem ipsum dolor sit amet" # Find all matches of the regular expression in the test string matches = re.findall(pattern, test_string) # Print the matches print(matches)
輸出
['L', 'o', 'r', 'e', 'm', 'i', 'p', 's', 'u', 'm', 'd', 'o', 'l', 'o', 'r', 's', 'i', 't', 'a', 'm', 'e', 't']
此示例演示瞭如何使用正則表示式和字元類在 Python 中匹配非空白字元。
示例
在 Python 正則表示式中匹配非空白字元的另一種方法是使用否定字元類。以下是一個示例
在此示例中,我們定義了一個正則表示式模式,該模式使用否定字元類 [^\s]
匹配單個非空白字元。字元類中的 ^
符號對該類取反,匹配任何不是空白字元的字元。然後,我們使用 re.findall()
函式查詢測試字串中正則表示式的所有匹配項並列印匹配項。
import re # Define a regular expression pattern to match a non-whitespace character using negative character classes pattern = r"[^\s]" # Define a test string test_string = "Lorem ipsum dolor sit amet" # Find all matches of the regular expression in the test string matches = re.findall(pattern, test_string) # Print the matches print(matches)
輸出
['L', 'o', 'r', 'e', 'm', 'i', 'p', 's', 'u', 'm', 'd', 'o', 'l', 'o', 'r', 's', 'i', 't', 'a', 'm', 'e', 't']
此示例演示了另一種使用正則表示式和否定字元類在 Python 中匹配非空白字元的方法。
使用 "\S" 模式匹配單個非空白字元
示例
import re text = "This is a test string. Let's see if we can match some non-whitespace characters!" #find the first non-whitespace character in the string match = re.search(r"\S", text) #print the match print(match.group())
輸出
T
使用 "\S+" 模式匹配一個或多個非空白字元的序列
示例
import re text = "This is a test string. Let's see if we can match some non-whitespace characters!" #find all sequences of one or more non-whitespace characters in the string matches = re.findall(r"\S+", text) #print the matches print(matches)
輸出
['This', 'is', 'a', 'test', 'string.', "Let's", 'see', 'if', 'we', 'can', 'match', 'some', 'non-whitespace', 'characters!']
使用 "[^ ]" 模式匹配特定字元範圍內的非空白字元
示例
import re text = "This is a test string. Let's see if we can match some non-whitespace characters!" #find all non-whitespace characters within the range of 'a' to 'z' in the string matches = re.findall(r"[a-z]+[^ ]*[a-z]+", text) #print the matches print(matches)
輸出
['his', 'is', 'test', 'string', "et's", 'see', 'if', 'we', 'can', 'match', 'some', 'non-whitespace', 'characters']
廣告