The regex seems quite complex by now do you mind moving the matching an regex construction to a different function? I get the feeling this will extend in the future so better keep it clean now.