jQuery in Action, 2nd edition*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Regex start() + group() question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Regex start() + group() question" Watch "Regex start() + group() question" New topic
Author

Regex start() + group() question

Paul Stat
Ranch Hand

Joined: Jan 20, 2009
Posts: 50
Given:



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

KS + BB 2006

Now I read this as find digits 0 to many, so that would match 34. Start() prints the start position of where the match is (2) and group prints what it's matched (34). So I get the answer A. 234

But the answers at the back say E. 01234456! How? I don't understand.
M Srilatha
Ranch Hand

Joined: Aug 27, 2008
Posts: 137
Hello,

Please refer to the following link:
how Regex works

Explanation is given very clearly!




Thanks,<br />Srilatha M
Paul Stat
Ranch Hand

Joined: Jan 20, 2009
Posts: 50
Aha I think i get it, because it's 0 or more, it will return the start of no match aswell. So you have



The position 6 thing is still a bit confusing mind you

Jia Tan
Ranch Hand

Joined: Jan 28, 2009
Posts: 32
Just ran it in Cygwin and it gave me empty as the result. The version I have is :

$ java -version
java version "1.6.0_03"
Java(TM) SE Runtime Environment (build 1.6.0_03-b05)
Java HotSpot(TM) Client VM (build 1.6.0_03-b05, mixed mode, sharing)

Ran under dos worked just fine. How come?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Regex start() + group() question
 
Similar Threads
K & B book- chapter 6 - excercie1-not able to understand
can some one explain me the output of this program?
K&B Study Guide for Java 5 p498 Selftest problem 1
SCJP book regex, pattern and matcher question
Regex Doubt