0

I have a string in Python like the following:

str = "Surname:.......1 of 10 Surname:........2 of 10 Surname.........3 of 10............."

I want to take every substring of the form:

"Surname:.......<number> of 10"

and edit it.

How could I take the n (above is 10) substrings that starts and end with the same form? I have tried the following :

    str = 'Surname:mike 1 of 3 Surname:uknown of 3 Surname:little 3 of 3'
    x = re.findall("Surname:.*[0-9]*\s*of", str)
    print(x)

but the result is :

['Surname:mike 1 of 3 Surname:uknown of 3 Surname:little 3 of 3']

and I want something like this :

['Surname:mike 1 of 3','Surname:unkown 2 of 3','Surname:little 3 of 3']

Of course I think there is a solution to iterate through the string but firstly I want to see if there is another solution with less code.

  • [re.search()](https://docs.python.org/3/library/re.html#re.search) looks only for the first hit. Use re.match() instead. In case it is still not working, please show us your regex and the code how you applied the regex. – cronoik Oct 02 '19 at 12:56

1 Answers1

0

By default, regex in python matches the largest possible thing. Add a ? to make it take the smallest possible thing:

x = re.findall("Surname:.*?[0-9]*\s*of", str)

The *? matches the smallest possible sequence of zero or more things, whereas * matches the largest possible sequence

Simon Crane
  • 2,122
  • 2
  • 10
  • 21