Java - Utility Classes - Interview Questions and Answers on Pattern / Regular Expressions

Q1.  What's wrong with this code ?

public static void main(String[] args) { 
       String regex = "(\\w+)*"; 
       String s = "Java is a programming language."; 
       
       Pattern pattern = Pattern.compile(regex); 
       Matcher matcher = pattern.matcher(s); 
       while (matcher.next()) { 
          System.out.println("The e-mail id is: " + matcher.group()); 
       }
}

Ans. matcher.find() should have been used instead of matcher.next() within while.

Q2.  Which methods of the Pattern class have equivalent methods in the String class? 

Ans. split() and macthes()