how to match whitespace and alphanumeric characters in python

做~自己de王妃 提交于 2019-12-21 07:03:14

问题


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:


import re

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

returns

('this matches',)


来源:https://stackoverflow.com/questions/5048091/how-to-match-whitespace-and-alphanumeric-characters-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!