From my understanding the . match almost all characters in regular expression. Then if I want to match any character including new line why [.\n]* does not work?
-
1Without information about which regex variant you're using, we have no way to tell you exactly why what you tried doesn't work, or what would work instead. You already have multiple answers, so it's really too late to fundamentally change this question. Accept an answer, read the [`regex` tag info page](/tags/regex/info), and if you need more help, ask a new question in accordance with the guidelines there. See also the [help] and perhaps in particular the instructions #or posting a [mcve]. – tripleee Aug 11 '18 at 19:12
3 Answers
Using [.\n]*
means a character class which will match either a dot or a newline zero or more times.
Outside the character class the dot has different meaning. You might use a modifier (?s)
or specify in the language or tool options to make the dot match a newline.

- 154,723
- 16
- 55
- 70
Most regular expression dialects define .
as any character except newline, either because the implementation typically examines a line at a time (e.g. grep
) or because this makes sense for compatibility with existing tools (many modern programming languages etc).
Perl and many languages which reimplement or imitate its style of "modern" regex have an option DOTALL which changes the semantics so that .
also matches a newline.
If you don't have that option, try (.|\n)*
but this still depends very much on which regex tool you are using; it might not recognize the escape code \n
for a newline, either.

- 175,061
- 34
- 275
- 318
You should rather use
.*\n -- this one if line can be empty
or
.+\n -- this one if line must include at least 1 character other that new line
also you should remember that sometimes format of newline can be \r\n
(Windows mostly)

- 13,268
- 18
- 37