Menu
Topics Index
...
`

Creating Pattern and Matcher


If you need explanation Read this topic

If you need Answer Take test on this topic

Creating Pattern and Matcher
What will be the output of the following program?
import java.util.regex.*;

public class RegTest {
    public static void main(String[] args) {
        String regex = "\\d";
        Pattern pattern = Pattern.compile(regex);
        String input = "Aaaa99MeritCampus4";
        Matcher matcher = pattern.matcher(input);
        matcher.find();
        matcher.find();
        System.out.print(matcher.group());
        matcher.find();
        System.out.println(matcher.group());
    }
}

A. 4
B. 9
C. 99
D. 94
E. 994
F. Compilation Error or Runtime Error
Topic: Java Regular Expression

If you need explanation Read this topic

If you need Answer Take test on this topic

User comments below. All of them might not be correct.

Answer : D 94
> We are trying to search for numbers (0-9) in the String input
> the second matcher.find() will return the number 9 at the Index 5 and matcher.group() will print it
> the third mathcer.find() will return the number 4 which is at the Index 17 and matcher.group() will print it

Posted by Deepak Kumar Yadav    2014-05-30 03:51:58


ans d. integer values from the input string searchd using matcher and find methods,
1st find method take value 9 ,4th indx.
2nd find take 9 from 5 th indx.
2nd find() value is printd.
3rd find() take 17th index value and printd

Posted by Maheshwari Natarajan    2014-05-30 04:06:12


Congratulations Deepak Kumar Yadav. You are this dose winner. We will send you the link using which you can claim your recharge.

Posted by Merit Campus    2014-06-02 06:57:50


© meritcampus 2019

All Rights Reserved.

Open In App