Which is the Best Java Web Framework ?

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()

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


Try our new Java Interview Questions Search Tool.

There are more than 1000 questions with practice tests.

It's updated daily and It's Awesome.


Try our new Java Practice Test tool.

There are more than 200 questions and expanding quickly. It's updated daily and It's Awesome.