Abood
Abood

Reputation: 41

How can I extract a multi-substrings from a string in python?

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.

Upvotes: 3

Views: 445

Answers (1)

Stefan Kögl
Stefan Kögl

Reputation: 4743

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
  • the ( indicates the start of the part (group) we're interested in
  • the . indicates arbitrary character
  • the +? indicates at least one, but as little as possible
  • the ) indicates the end of the part (group) wer're interested in
  • a space followed by end.

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.

Upvotes: 4

Related Questions