DingDong
DingDong

Reputation: 13

Python how to find a list of string that's inbetween 2 substring?

I'm trying to get a list of string that's inbetween 2 string

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 # 

But I want to be able to search thru the string and get a list that found...something like

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

Upvotes: 1

Views: 56

Answers (3)

Weafs.py
Weafs.py

Reputation: 22992

Why not use the re module(regular expression) to do it?

--------------------------------------------------------- Solution: 1 -----------------------------------------------------------

Demo on repl.it

Code:

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

Output:

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

RegEx Explained:

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

\$: This will match $(This character needs to be escaped).
\/: This will match /(This character needs to be escaped).
( : This represents the beginning of extraction.
. : This will match any character except a new line.
+ : This represents match 1 or more previous character.
? : This will do a non-greedy search.
) : This represents the end of extraction.
: : This will match :.

--------------------------------------------------------- Solution: 2 -----------------------------------------------------------

Demo on repl.it

Code:

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('$/', ':')

Output:

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

Upvotes: 1

Mark Tolonen
Mark Tolonen

Reputation: 177674

Here's a way to continue on your attempt and a generalized example with re.findall as well:

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,'$/',':'))

Output:

['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']

Upvotes: 0

Ian Price
Ian Price

Reputation: 7616

I'm not entirely clear from your question, but assume you're looking for something like this...

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

To illustrate...

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

will display

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

Upvotes: 1

Related Questions