Python正则表达式re.findall - 将字符串拆分为两个

时间:2013-12-08 01:09:56

标签: python regex string python-2.7

我有这样的字符串:

“C BOS - 从皇家失望交易”

我希望在短跑之前将它们分成所有东西,之后的一切。所以简单地在两个变量new1和new2中,我希望上面是:

new1 =“C BOS”

new2 =“皇家失望交易”

我是新来的,所以我无法弄清楚如何让findall工作。这就是我的尝试:

import re

myval = "C BOS  - Traded from Royal Disappointments"

new1 = re.findall( r'*\s\s\-',  myval )
new2 = re.findall( r'-\s*', myval)

我知道它可能并不接近,但我不清楚如何表达。

2 个答案:

答案 0 :(得分:3)

只要您的字符串示例成立,我就会在没有重复的情况下执行此操作。

需要注意的事项:多于一个'-'或没有'-'

您可能希望处理拆分和分配的可能异常。

>>> example = "C BOS  - Traded from Royal Disappointments"
>>> before, after = example.split('-')
>>> before = before.strip()
>>> after = after.strip()
>>> print before
C BOS
>>> print after
Traded from Royal Disappointments
>>>

答案 1 :(得分:2)

使用re.split

>>> import re
>>> s = "C BOS  - Traded from Royal Disappointments"
>>> re.split(r'\s*-\s*', s)
['C BOS', 'Traded from Royal Disappointments']

将结果分配给变量:

>>> new1, new2 = re.split(r'\s*-\s*', s)
>>> new1
'C BOS'
>>> new2
'Traded from Royal Disappointments'

非正则表达式版本,但需要两次传递:

>>> map(str.strip, s.split('-'))
['C BOS', 'Traded from Royal Disappointments']

如果字符串包含多个-并且您仍想要只拆分一次,那么将拆分计数传递给re.split

>>> s = "C BOS  - Traded from Royal Disappointments  -  foobar"
>>> re.split(r'\s*-\s*', s, 1)
['C BOS', 'Traded from Royal Disappointments  -  foobar']