2016-02-01 254 views
1

我想在python中使用re模块来分割表示列表的字符串。该列表由括号标识。如何用正则表达式分割python中的括号列表?

输入:

"[1]first[2]second[3]third" ... etc 

所需的输出:

['first', 'second', 'third',...] 

我当前的代码如下:

out = re.split('\[(.*?)\]', thelist) 

它返回以下,但如何获得想要的?

['', '1', 'first', '2', "second", '3', 'third',...] 
+0

如果什么列表元素包含'[数字]'? –

回答

2

您可以使用正则表达式匹配附带[...]数字和摆脱空元素的搭配:

import re 
p = re.compile(r'\[\d+\]') 
test_str = "[1]first[2]second[3]third" 
print([x for x in p.split(test_str) if x]) 
# => ['first', 'second', 'third'] 

IDEONE demo

您的代码返回因为re.split返回所有捕获的文本作为结果数组中的独立元素捕获。

如果分隔符中存在捕获组,并且它在字符串的开头匹配,则结果将以空字符串开头。

而且,要摆脱仅仅是第一个空的元素,你可以使用

res = p.split(test_str) 
if not res[0]: 
    del res[0] 
+0

如果列表中没有括号并且看起来像这样: 1.sdjdjdj2.sdjsdjjsd3.sdjdjds54.sdjsd 列表编号之前可以有数字吗? – Shruf

+0

然后,我会使用['p = re.compile(r'\ d + \。')'](http://ideone.com/gUlB6e)。 –

+0

我试过了,但问题是它带走了54.当它应该只是拿走了列表中的最后一个元素。 另一个例子1.kk2.y63。tt - > [kk,y6,tt]而不是[kk,y,tt] – Shruf

1

使用了[2:2]。这需要从第三个到最后每个条目,但只需要每隔一个条目。

1

如果格式总是相同的,你没有在说话括号,然后使用的findall并得到串,每个闭合支架后:

s = "[1]first[2]second[3]third" 

import re 

print(re.findall("\](\w+)" ,s)) 
['first', 'second', 'third'] 

要处理的空间等。您可以使用字符集:

s = "[1]first foo[2]second[3]third" 

import re 

print(re.findall("\]([\w\s]+)", s)) 
['first foo', 'second', 'third'] 
1

您可以使用简单的regex,如果你的字符串看起来您所描述的方法:

re.findall(r'[a-z]+', s) 

findall将返回给你一个列表,因此无需split

和输出:

['first', 'second', 'third']