How to match a non-whitespace character in Python using Regular Expression?


In Python, a non-white space character is any character that is not a space, tab, or newline. These characters are important for formatting and readability in Python code.

Suppose we have a string with both whitespace and non-whitespace characters: We can use the isspace() method to check if each character in the string is a whitespace character

In this code, we iterate over each character in the my_string variable and use the isspace() method to determine if the character is a whitespace character or not. If the character is a whitespace character, we print "Whitespace character", and if it is a non-whitespace character, we print "Non-whitespace character".

Example

my_string = "Hello,     world!"
for char in my_string:
    if char.isspace():
        print("Whitespace character")
    else:
        print("Non-whitespace character")

Output

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

In this output, we can see that the whitespace characters (spaces) are identified by the isspace() method, while the non-whitespace characters (letters, commas, and exclamation marks) are not.

Using Character Classes

One way to match a non-whitespace character in Python regular expression is by using character classes. Here is an example:

Example

In this example, we define a regular expression pattern that matches a single non-whitespace character using the \S metacharacter. We then use the re.findall() function to find all matches of the regular expression in the test string and print the matches.

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)

Output

['L', 'o', 'r', 'e', 'm', 'i', 'p', 's', 'u', 'm', 'd', 'o', 'l', 'o', 'r', 's', 'i', 't', 'a', 'm', 'e', 't']

This example demonstrates how regular expressions can be used to match a non-whitespace character in Python using character classes.

Example

Another way to match a non-whitespace character in Python regular expression is by using negative character classes. Here is an example:

In this example, we define a regular expression pattern that matches a single non-whitespace character using the negative character class [^\s]. The ^ symbol inside the character class negates the class, matching any character that is not a whitespace character. We then use the re.findall() function to find all matches of the regular expression in the test string and print the matches.

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)

Output

['L', 'o', 'r', 'e', 'm', 'i', 'p', 's', 'u', 'm', 'd', 'o', 'l', 'o', 'r', 's', 'i', 't', 'a', 'm', 'e', 't']

This example demonstrates another way of using regular expressions to match a non-whitespace character in Python using negative character classes.

Matching a single non-whitespace character using the "\S" pattern:

Example

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())

Output

T

Matching a sequence of one or more non-whitespace characters using the "\S+" pattern

Example

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)

Output

['This', 'is', 'a', 'test', 'string.', "Let's", 'see', 'if', 'we', 'can', 'match', 'some', 'non-whitespace', 'characters!']

Matching non-whitespace characters within a specific range of characters using the "[^ ]" pattern

Example

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)

Output

['his', 'is', 'test', 'string', "et's", 'see', 'if', 'we', 'can', 'match', 'some', 'non-whitespace', 'characters']

Updated on: 19-May-2023

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements