An easy way to fix this issue is replacing newline characters with a space character before applying your regular expression, or replacing them with symbols that don't occur in the data naturally, so that you can restore them later if needed.