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 Doubt 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 "Regex Doubt" Watch "Regex Doubt" New topic
Author

Regex Doubt

Mercurio Savedra
Greenhorn

Joined: Dec 04, 2005
Posts: 25
Hello Friends,

This is Question for K & B book...Chap 6 : Page 508

code:

Given:
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:

E is correct. The \d is looking for digits. The * is a quantifier that looks for 0 to many occurrences of the pattern that precedes it. Because we specified *, the group() method returns empty Strings until consecutive digits are found, so the only time group() returns
a value is when it returns 34 when the matcher finds digits starting in position 2. The start() method returns the starting position of the previous match because, again, we said find 0 to many occurrences.



Why this code doesnt fail, shouldnt be the regex expresion something like "\\d*" instead "\d*" in order to inform the compiler that this is not an escape character
Sasha Ruehmkorf
Ranch Hand

Joined: Mar 29, 2007
Posts: 115
you want to inform the compiler? why disturb him when there's nothing to do?

you are right, if the regex-expression wasn't given per command-line, you had to write "\\d*"
Mercurio Savedra
Greenhorn

Joined: Dec 04, 2005
Posts: 25
but sasha i think thatin said that is you pass by command line the expresion "\d*" it must be "\\d*" too

tell me if i�m wrong

thank
Mercurio Savedra
Greenhorn

Joined: Dec 04, 2005
Posts: 25
excuse me i write again without nistakes

but sasha i think that in K & B said that is you pass by command line the expresion "\d*" it must be "\\d*" too

tell me if i�m wrong
Pravin Jain
Ranch Hand

Joined: May 20, 2007
Posts: 60
The "\\d*" is required inside source code for the compiler,
when you compile the code the String is converted to \d*
which has 3 characters by the compiler. regex requires the string as \d*
What you give as command line arguments is a string given at runtime
and it taken as is.


The Zen of Java Programming.
Kram Nart
Ranch Hand

Joined: Jun 05, 2006
Posts: 32
I have a different question for the code sample above.

I didn't expect the trailing 6 at the end of that answer. I thought it would be 0123445 and not 01234456. Once the m.group() displays 34, the next output of 4 is the result of m.find() finding a match for the character e. Then 5 gets outputted from m.find() finding a match for the character f.

Can someone clarify?


SCJP 5 <br /> SCWCD 5 <br /> SCBCD <br />
Figuring out what's next...
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41816
    
  62
Kram,

please do not hijack threads with questions that have nothing to do with the original topic. Your question is answered in the SCJP FAQ.


Ping & DNS - my free Android networking tools app
 
 
subject: Regex Doubt