Python如何查找2个子字符串之间的字符串列表?

时间:2014-10-20 04:16:11

标签: python

我正在尝试获取两个字符串之间的字符串列表

text = """
something = $/I want to get this string A:blah blah 
somethingB = ($/I want to get this string as well B:blah blah blah)
"""

#I had this working to get the first found string
def find_between( s, first, last ):
    try:
        start = s.index( first ) + len( first )
        end = s.index( last, start )
        return s[start:end]
    except ValueError:
        return ""


find_between(text, '$/', ':')
# Result: I want to get this string A # 

但我希望能够通过字符串搜索并获得一个找到的列表......类似于

['I want to get this string A', 'I want to get this string as well B']

3 个答案:

答案 0 :(得分:1)

为什么不使用re模块(正则表达式)来做呢?

----------------------------------------------- ----------解决方案:1 ------------------------------------- ----------------------

repl.it

上的演示

代码:

import re

text = """
something = $/I want to get this string A:blah blah 
somethingB = ($/I want to get this string as well B:blah blah blah)
"""

found = re.findall(r'\$\/(.+?)\:', text)
print found

输出:

['I want to get this string A', 'I want to get this string as well B']

RegEx解释:

r'\$\/(.+?):'
  

\$:这将匹配$(此字符需要转义)   \/:这将匹配/(此字符需要转义)   (:这代表了提取的开始   .:这将匹配除新行之外的任何字符   +:这表示匹配1个或多个前一个字符   ?:这将进行非贪婪的搜索   ):这表示提取结束   ::这将与:匹配。

----------------------------------------------- ----------解决方案:2 ------------------------------------- ----------------------

repl.it

上的演示

代码:

import re

text = """
something = $/I want to get this string A:blah blah 
somethingB = ($/I want to get this string as well B:blah blah blah)
"""

def findBetween(first, second):
    first = '\\' + first[0] + '\\' + first[1]
    found = re.findall(r'' + first + '(.+?)' + second, text)
    print found

findBetween('$/', ':')

输出:

['I want to get this string A', 'I want to get this string as well B']

答案 1 :(得分:1)

我的问题并不完全清楚,但假设你正在寻找类似的东西......

def find_betweens(text,start,end):
    betweens = []
    for i in text.split(start):
        if end in i:
            betweens.append(i.split(end)[0])
    return betweens

说明......

yo = "Hello. How are you? I'm good. You too?"
print find_betweens(yo, ".", "?")

将显示

[' How are you', ' You too']

答案 2 :(得分:0)

以下是继续尝试的方法以及re.findall的广义示例:

import re

text = '''\
something = $/I want to get this string A:blah blah 
somethingB = ($/I want to get this string as well B:blah blah blah)
'''

def find_between1(s,first,last):
    first = re.escape(first)
    last = re.escape(last)
    return re.findall(first + r'(.*?)' + last, s)

def find_between2(s, first, last):
    start = 0
    L = []
    while True:
        try:
            start = s.index(first, start) + len(first)
            end = s.index(last, start)
            L.append(s[start:end])
            start = end + len(last)
        except ValueError:
            return L

print(find_between1(text,'$/',':'))
print(find_between2(text,'$/',':'))

输出:

['I want to get this string A', 'I want to get this string as well B']
['I want to get this string A', 'I want to get this string as well B']