pythonregexpython-3.xdelimitermultiple-matches

Find all repeating strings between a start and a stop and with a separator


I'm trying to get one or more substring from a string like this :

"theSTART:param1:param2:paramN:theEND"

or

"theSTART:param1:theEND"

or any number of parameter saparated by ":" and delimited by "theSTART:" and ":theEND". I'm using Python3 regex but I can't find the right pattern to match a variable number of items:

r"theSTART:((?:\w*)+):((?:[^:])*):((?:\w)+):theEND"

works only for 3 items. And if I try :

r"theSTART:((?:\w*)+):(((?:[^:])*):)+((?:\w)+):theEND"

doesn't work as expected.


Solution

  • re.findall might be a solution if you want to use regular expressions instead of splitting. findall returns a list of all occurrences of a pattern.

    re.findall(r":(?:(\w+)(?=:))", r"theSTART:param1:param2:paramN:theEND")
    

    returns the list ['param1', 'param2', 'paramN'].

    You probably won't (or can't) find a pattern to match any number of parameters. According to the Python re module documentation is only the last match accessible, if a group matches multiple times.