0

I have a case where I have to break down a string that looks something like this:

TASK **********************************  
everything ok 
TASK **********************************  
some text here untill you get dot retry.retry  
TASK **********************************  
everything ok

I want to capture only groups that have failed (have .retry at the end)

TASK **********************************  
some text here untill you get dot retry.retry  

So far I've come closest to what I need with the following regex, however, it only captures the first group and only the first group

(?m)(TASK.*\.retry)  

Any suggestions?

edit:

re.findall(r"TASK.+?\.retry" , text, flags = re.DOTALL)

will find groups if they all end with .retry, this is how the question was originally phrased, but was wrong... my bad.

edit 2:
the duplicate answer does not exclude the groups that are ok, why is this flagged?

Mazimer
  • 33
  • 7

1 Answers1

1

By break down you want every Task until retry.

re.findall(r"TASK.+?\.retry" , text, flags = re.DOTALL)

Changes :

Since you have to get only the retry parts of the result, You can get it with a different approach.

list_obj = string.split("retry.retry")
repeatable_tasks = []
for each_obj in list_obj[:-1]:
    repeatable_tasks.append("".join(each_obj.rpartition("TASK")[1:]))
Kenstars
  • 662
  • 4
  • 11
  • `r"(TASK[\s\S]+?\.retry)"` is an alternative that does not require `DOTALL`, in case he wants to use default dot behaviour elsewhere. – Zinki Apr 12 '18 at 13:59
  • @Zinki but wouldnt this capture it from the first TASK to the last retry in a single group . – Kenstars Apr 12 '18 at 14:02
  • sorry, i updated the question to reflect what i actually need. my bad for not clarifying. this indeed finds the groups but will not exclude the ones that are ok (which is the edit on the original question) – Mazimer Apr 12 '18 at 14:13
  • It wouldn't for the same reason your answer doesn't (using non-greedy operator `?`). `.` with DOTALL flag is equivalent to `[\s\S]`. – Zinki Apr 12 '18 at 14:13
  • this doesnt' split up the groups properly, i end up getting 1 match (that is too long): `TASK ********************************** everything ok TASK ********************************** some text here untill you get dot retry.retry` – Mazimer Apr 12 '18 at 14:28
  • @Mazimer, look at the changes added to the answer, hopefully that solves your problem – Kenstars Apr 12 '18 at 14:38