Solution to 'Generate String from Regex' that passes all test cases