• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Solve this question regarding (regex) Pattern

 
sriannapoorna Balasubramanian
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is a Frequently Asked Question
 
Marcus Green
arch rival
Rancher
Posts: 2813
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic