I need help with extracting multiple substrings from a string and assign it to a list, and those required substrings have similar start substring and end substring,
for example if I have a string like this: "start something something end start nothing nothing end"
how do I extract "something something" and "nothing nothing" and append it to a list.
You can use re.findall.
>>> text = 'start something something end start nothing nothing end'
>>> re.findall('start (.+?) end', text)
['something something', 'nothing nothing']
Return all non-overlapping matches of pattern in string, as a list of strings.
Pattern is
start followed by a space( indicates the start of the part (group) we're interested in. indicates arbitrary character+? indicates at least one, but as little as possible) indicates the end of the part (group) wer're interested inend.The string is scanned left-to-right, and matches are returned in the order found.
If one or more groups are present in the pattern, return a list of groups;
We're using groups, as shown above
this will be a list of tuples if the pattern has more than one group.
We only use one group.
Empty matches are included in the result.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With