Corrected substringBetweenMatches function