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

PythonServer Side ProgrammingProgramming

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']
raja
Updated on 18-Feb-2020 07:51:27

Advertisements