File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes regex in SCJP test 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 in SCJP test" Watch "regex in SCJP test" New topic
Author

regex in SCJP test

tural ferhadov
Greenhorn

Joined: Oct 20, 2012
Posts: 7
hello,
I am trying to solve an scjp test about regex.

here is a code...

import java.util.regex.*;

public class TestRegex{
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
java TestRegex "\d*" ab34ef
the answer is 01234456. I understood everything except the last output(6). Since the last index in "ab34ef" is 5, how is it possible to be printed 6 ?

Any help ....


Nancy Avent
Ranch Hand

Joined: Feb 23, 2013
Posts: 94
tural ferhadov wrote:hello,
I am trying to solve an scjp test about regex.

here is a code...

import java.util.regex.*;

public class TestRegex{
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
java TestRegex "\d*" ab34ef
the answer is 01234456. I understood everything except the last output(6). Since the last index in "ab34ef" is 5, how is it possible to be printed 6 ?

Any help ....




Here is my try:
The expression "\d*" refers to any digit of length 0 or more; thus, it will match to all zero size matches too. The first match start before 'a' and end before 'a' (size zero match); this will print 0. What it means is the last match would be after 'f' (size zero match) and thus it will result in 6.
Adam Crawford
Greenhorn

Joined: Feb 27, 2012
Posts: 9
Take a look at this - I found this really useful when I was confused about the same thing.

Good luck!
tural ferhadov
Greenhorn

Joined: Oct 20, 2012
Posts: 7
Thank you guys, it really helped
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: regex in SCJP test
 
Similar Threads
Doubt in question 1 chapter 6 of K&B SCJP 5 Study Guide
Pattern Class Doubt
Regex SCJP 5.0
quantifier
Java regexp