Maybe I'm not thinking straight (not a lot of sleep lately), but could you skip the loop here and just run the regex search on the whole of the `output`?