如何在python中匹配空格和字母数字字符 [英] how to match whitespace and alphanumeric characters in python
本文介绍了如何在python中匹配空格和字母数字字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试匹配中间有空格和字母数字字符的字符串,如下所示:
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'
不幸的是,它只匹配空格,所以当使用 re 对象中的搜索方法找到匹配项时,它只返回空格,而不是整个字符串,我如何更改模式以便在以下情况下返回整个字符串找到匹配项?
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()
返回
('this matches',)
这篇关于如何在python中匹配空格和字母数字字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文