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 ( { 
          System.out.println("The e-mail id is: " +; 

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

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

Ans. split() and macthes()

Subscribe to Java News and Posts. Get latest updates and posts on Java from
Enter your email address:
Delivered by FeedBurner
comments powered by Disqus