如何使用正则表达式在 Python 中匹配非空白字符?


在 Python 中,**非空白字符**是指任何不是空格、制表符或换行符的字符。这些字符对于 Python 代码的格式和可读性非常重要。

假设我们有一个包含空白字符和非空白字符的字符串:我们可以使用 isspace() 方法检查字符串中每个字符是否为空白字符。

在此代码中,我们遍历 my_string 变量中的每个字符,并使用 isspace() 方法确定该字符是否为空白字符。如果该字符为空白字符,则打印“空白字符”,如果是非空白字符,则打印“非空白字符”。

示例

Open Compiler
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() 函数查找测试字符串中正则表达式的所有匹配项并打印匹配项。

Open Compiler
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() 函数查找测试字符串中正则表达式的所有匹配项并打印匹配项。

Open Compiler
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" 模式匹配单个非空白字符

示例

Open Compiler
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

Learn Python in-depth with real-world projects through our Python certification course. Enroll and become a certified expert to boost your career.

使用 "\S+" 模式匹配一个或多个非空白字符的序列

示例

Open Compiler
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!']

使用 "[^ ]" 模式匹配特定字符范围内的非空白字符

示例

Open Compiler
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']

更新于: 2023年5月19日

3K+ 浏览量

开启你的 职业生涯

通过完成课程获得认证

开始学习
广告