mardi 22 novembre 2016

If statement that is supposed to filter out newline not filtering new lines.

I'm writing a Regex pattern that filters through HTML tags and prints only the contents of valid tags for practice. While the pattern itself appears to be matching tags correctly, I am running into an issue when printing them.

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class HTMLPattern{
   public static void main(String[] args){

      Scanner in = new Scanner(System.in);
      int testCases = Integer.parseInt(in.nextLine());

      while(testCases>0){
          String line = in.nextLine();
          String tagPattern = "<([^>]+)>([^<]*?)</\\1>";
          Pattern p = Pattern.compile(tagPattern, Pattern.MULTILINE);
          Matcher m = p.matcher(line);
          if(m.find()){
              //checks if the output equals a newline
              if(m.group(2).matches("[\\n\\r]+")){
                   System.out.println("None");     
              }else{
                   System.out.println(m.group(2));
              }
          }else{
              System.out.println("None");
          }
         testCases--;
      }
   }
}

When inputting:

3
<a>test</a>
<b></b>
<c>test</c>

My output should be:

test
None
test

But instead it is:

test

test

My question is: Why is my if-statement not catching the newline and printing "None"?

Aucun commentaire:

Enregistrer un commentaire