You can do this with XPath and do retrieve only on those items that have the text attribute "Erorr".
All can be done with string function calls: https://docs.mendix.com/refguide/string-function-calls/
Use the find method to get the location of Error. So
find($stringtosearch, 'Error')
The result would be 47. Now you wanted the string before the error so now find the position of Good. Since you need the last occurance of Good before 47 you could use substring for that. We already now the length of the string we want (47) so just use the following command. Note that 0 means start from the biginning.
substring($stringtosearch, 0,47)
You can then use findLast to get the last position of Good. You can then use substring again but instead of 0 use the value of the position of Good plus 4 for the length of Good to get the result you want.
This way you can cut your string up untill there are no Errors in you string anymore.
Regards,
Ronald