If I want to search for a newline in a text using Java, should my regex be "\n"
or "\\n"
, or will either work?
Asked
Active
Viewed 101 times
-5

Ypnypn
- 933
- 1
- 8
- 21
-
5And you didn't find answer of this question by searching? – anubhava May 05 '14 at 21:06
-
try them both on small text passage with newline and you will find out which one works. – akhilless May 05 '14 at 21:08
-
possible duplicate of [Can't escape the backslash with regex?](http://stackoverflow.com/questions/4025482/cant-escape-the-backslash-with-regex) – melli-182 May 05 '14 at 21:11
-
Can you try this yourself? Then post the answer so that we'll all know. – Dawood ibn Kareem May 05 '14 at 21:56
1 Answers
1
You aren't matching a backslahs (\
) and an n
, you're matching a newline character (represented as \n
). There's no need to escape the backslash:
Pattern p = Pattern.compile(".*\n.*");

Mureinik
- 297,002
- 52
- 306
- 350
-
It shouldn't be `.*\n` only ? If not, what are you matching after the new line ? – Pedro Lobito May 05 '14 at 21:19