import re
s6 = '''Male : abc
Male : def'''
re.findall(r'(.*):|:(.*)', s6)
我的输出:
[('Male ', ''), (' Male ', '')]
我的期望输出:
[('Male','abc'),('Male','def')]
当我使用
re.findall(r'(.*):', s6)
时,我得到的是['Male ', ' Male ']
,而当我使用re.findall(r':(.*)', s6)
时,我得到的是[' abc', ' def']
。当我使用|
符号时,输出结果是错误的。免责声明:请勿使用
re.split
我已经使用了\w+
也能够工作,但我想要一个以下格式的表达式。我已经使用了re.findall(r'\s*(\w+)\s*:|:\s*(\w+)', s6)
,但它不能单独工作。使用\s*(\w+)\s*:
和:\s*(\w+)', s6
时,它可以工作。我想知道为什么使用
|
时它不能工作。