这就是我想要的东西
split('number1+number2')
-->['number1', '+', 'number2']
和
split('number1 * number2')
-->['number1', '*', 'number2']
我想分开*,+和 - ,但保留它们。所以我试着做re.split('(\W+)', mystring)
,虽然有效,但它也保留了空白。我该怎么办?
答案 0 :(得分:1)
[^\w\s]
匹配既不是单词字符也不是空格的字符,因此您可以使用
>>> re.split(r"\s*([^\w\s]+)\s*",'number1 * number2')
['number1', '*', 'number2']
或者,作为一个冗长的正则表达式:
re.split(
r"""\s* # Match (but don't capture) optional whitespace characters
( # Capture...
[^\w\s]+ # one or more non-whitespace, non-alphanumeric characters
) # End of capturing group
\s* # Match (but don't capture) optional whitespace characters""",
'number1 * number2', flags=re.VERBOSE)
答案 1 :(得分:1)
In [13]: import re
In [14]: re.split(r'\s*([*+-])\s*',s)
Out[14]: ['number1', '*', 'number2']
In [15]: re.split(r'\s*([*+-])\s*','number1 * number2')
Out[15]: ['number1', '*', 'number2']
In [16]: re.split(r'\s*([*+-])\s*','number1+number2')
Out[16]: ['number1', '+', 'number2'
答案 2 :(得分:0)
答案 3 :(得分:0)
您只需使用re.findall
:
>>> s1 = 'number1+number2'
>>> s2 = 'number1 * number2'
>>> s3 = 'number1 * number2*number3- foo -bar'
>>> r = re.compile(r'\w+|[*+-]')
>>> r.findall(s1)
['number1', '+', 'number2']
>>> r.findall(s2)
['number1', '*', 'number2']
>>> r.findall(s3)
['number1', '*', 'number2', '*', 'number3', '-', 'foo', '-', 'bar']
答案 4 :(得分:0)
您可以使用 ungreedy 匹配:
re.split("\s*(\W+?]+)\s*",'number1 * number2')
正确地给出['number1', '*', 'number2']