wood burning stoves
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
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Solve this question regarding (regex) Pattern" Watch "Solve this question regarding (regex) Pattern" New topic

Solve this question regarding (regex) Pattern

sriannapoorna Balasubramanian

Joined: Aug 24, 2006
Posts: 9
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

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


SCWCD: Online Course, 50,000+ words and 200+ questions
I agree. Here's the link: http://aspose.com/file-tools
subject: Solve this question regarding (regex) Pattern
It's not a secret anymore!