how to match whitespace and alphanumeric characters in python


Question

I'm trying to match a string that has a space in the middle and alphanumeric characters like so:

test = django cms

I have tried matching using the following pattern:

patter = '\s'

unfortunately that only matches whitespace, so when a match is found using the search method in the re object, it only returns the whitespace, but not the entire string, how can I change the pattern so that it returns the entire string when finding a match?

1
16
10/21/2017 9:09:24 AM

Accepted Answer

import re

test = "this matches"
match = re.match('(\w+\s\w+)', test)
print match.groups()

returns

('this matches',)
39
2/19/2011 1:23:09 AM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon