Py学习  »  Python

python regex:最多有一个空格的字符串

Oliver Wilken • 6 年前 • 1685 次点击  

你好,我想知道如何创建一个regex模式与一个刺可能包含最多一个空白。更具体地说:

s = "a    b d d  c"
pattern = "(?P<a>.*) +(?P<b>.*) +(?P<c>.*)"
print(re.match(pattern, s).groupdict())

返回:

{'a': 'a    b d d', 'b': '', 'c': 'c'}

我想要:

{'a': 'a', 'b': 'b d d', 'c': 'c'}
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/34271
文章 [ 5 ]  |  最新文章 6 年前