0

Consider following code.

String s = "000000Xabcdefgh\nijkX00000000";
s = s.replaceAll("X.*X", "");
System.out.println(s);

I expect Xabcdefgh\nijkX to get replaced with an empty string, but since there is a newline in the middle, nothing gets replaced. Why is the regex matching terminated at newline? How can I ignore newlines when matching regex?

Lahiru Chandima
  • 22,324
  • 22
  • 103
  • 179

1 Answers1

0

try this:

String s = "000000Xabcdefgh\nijkX00000000";
s = s.replaceAll("X.*( |\t|\r\n|\r|\n).*X", "");
System.out.println(s);

This will take care of all other white-space/newline chars.

Sachin Gupta
  • 7,805
  • 4
  • 30
  • 45