获取2个其他字符串之间的字符串 - Python 2.7.8

时间:2014-10-23 11:26:13

标签: python string

所以我有一个巨大的字符串,其中一些字符串出现很多。我需要两者之间的文字。

"I don't need this""This is what I need""I also don't need this."

这种情况多次发生,我希望列表中需要的所有字符串。

还有很多特殊字符,但没有'所以我可以将它们用于字符串。

我已尝试使用re库,但我无法使用它。

我也试过分裂

listy = hugestring.split('delim1')
for element in listy:
    element = element.split('delim2')

但是第二次分裂不起作用。

3 个答案:

答案 0 :(得分:1)

您可以使用像这样的正则表达式

>>> import re
>>> your_str = "foo This is what I need bar foo This is what I need too bar"
>>> left_delim = "foo "
>>> right_delim = " bar"
>>> pattern  = "(?<={})[ \w]*?(?={})".format(left_delim,right_delim)
>>> re.findall(pattern,your_str)
['This is what I need', 'This is what I need too']

答案 1 :(得分:0)

这将为您提供字符串中包含引号的所有字符串的列表:

import re
in_str = "I don't need this\"This is what I need\"I also don't need this."
out_str = re.findall(r'\"(.+?)\"', in_str)
print out_str

所以在上面的例子中,print out_str[0]会给你你需要的东西,因为那里只有一个引号。

答案 2 :(得分:0)

这是你在评论中说的结果,所以现在问题是什么?:

>>> n= s.split("I don't need this")
['', "This is what I needI also don't need this."]
>>> [i.split("I also don't need this") for i in n]
[[''], ['This is what I need', '.']]