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
Published on 29-Dec-2017 13:47:57
Advertisements