-2

I have project to detect if editor have write html entities, but when it containt \n it doesnt work? why?

import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegexTest {  
   public static void main(String[] args) {
      String text = "asdasdas &lt;h1&gt;Test&lt;/h1&gt;</div>";
      String regex = ".*&lt;[^&lt]+&gt;.*";
      Pattern pattern = Pattern.compile(regex);
      Matcher m = pattern.matcher(text);
      System.out.println(m.matches());
   }
}

1 Answers1

0

If you want to take \n into consideration, you can do this:

Pattern pattern = Pattern.compile(regex, Pattern.DOTALL);

This takes the escape sequence into consideration.

You can also use Pattern.MULTILINE, which matches the regex with Each Line. So if you add ^ or $ in your regex, it matches the starting and ending of the regex respctively for each new line.

This is a link to the Oracle docs which may help you better understand, rather than just application of the code. The More You Know... :)

Robo Mop
  • 3,485
  • 1
  • 10
  • 23