This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Solve this question regarding (regex) Pattern Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Solve this question regarding (regex) Pattern" Watch "Solve this question regarding (regex) Pattern" New topic
Author

Solve this question regarding (regex) Pattern

sriannapoorna Balasubramanian
Greenhorn

Joined: Aug 24, 2006
Posts: 9
Hi,
This Question is regarding patterns in SCJP5.0.I am confused with the output.please explain the output step by step.

Question :

import java.util.regex.*;
class Regex2 {
public static void main(String[] args) {
Pattern p = Pattern.compile(args[0]);
Matcher m = p.matcher(args[1]);
boolean b = false;
while(b = m.find()) {
System.out.print(m.start() + m.group());
}
}
}

And the command line:
java Regex2 "\d*" ab34ef

What is the result?
A. 234
B. 334
C. 2334
D. 0123456
E. 01234456
F. 12334567
G. Compilation fails.

Answer is E.

* indicates zero or more occurence.
m.start will return position = 012345
I am not clear with concept of m.group.I refered to api,but still i am not clear.
Please explain in step by step fashion.
Thanks in advance.
[ August 30, 2006: Message edited by: Barry Gaunt ]
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
This is a Frequently Asked Question


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Marcus Green
arch rival
Rancher

Joined: Sep 14, 1999
Posts: 2813
You should probably refer to this topic as regular expressions rather than patterns, otherwise people may confuse it with Design Patterns (a subject that is not on the SCJP exam).

Marcus


SCWCD: Online Course, 50,000+ words and 200+ questions
http://www.examulator.com/moodle/course/view.php?id=5&topic=all
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Solve this question regarding (regex) Pattern
 
Similar Threads
Chapter 6 Self Test Q1
Regex Question
Regex Question from K&B book
quantifiers
Need Explanation