Computer >> Computer tutorials >  >> Programming >> Python

How to match a nonwhitespace character in python using Regular Expression?


The following code matches all non-whitespace characters in the given string.

Example

import re
foo = re.search(r'\S+', 'Need for Speed 2')
print foo

output

<_sre.SRE_Match object at 0x0000000004A06648>

Example

The following code matches and finds all non-whitespace characters in the given string and prints them

import re
foo = re.findall(r'\S+', 'Need for Speed 2')
print foo

output

['Need', 'for', 'Speed', '2']