开发者

how to match whitespace and alphanumeric characters in python

开发者 https://www.devze.com 2023-02-12 04:42 出处:网络
I\'m trying to match a string that has a space in the middle and alphanumeric characters like so: test = django cms

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?


import re

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

returns

('this matches',)


In case there is more than one space use the following regexp:

'([\w\s]+)'

example

In [3]: import re

In [4]: test = "this matches and this"
   ...: match = re.match('([\w\s]+)', test)
   ...: print match.groups()
   ...: 
('this matches and this',)
0

精彩评论

暂无评论...
验证码 换一张
取 消