regex to flatten the input