SCJP 5.0 (90%), SCDJWS 1.4 (88%), SCWCD 1.4 (82%), SCBCD 1.3 (85%)
Originally posted by Watsh Rajneesh:
Hi all,
I could not get the question 1 of chapter 6 in K&B scjp 5.0 study guide working. Can someone explain whats missing?
Here's is the question:
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
Answer is:
E. 01234456
But when i run this code there is nothing printed out. The matcher m does not return true on find().
But if use the regex pattern "\d?" for the same program then i do get an o/p as:
012334456
Though the logic explained in the book for the pattern "\d*" seems reasonable but i am not sure what is wrong about the code that m.find() returns false always.
Any explanation is much appreciated.
Thanks
Watsh
SCJP 5.0 (90%), SCDJWS 1.4 (88%), SCWCD 1.4 (82%), SCBCD 1.3 (85%)
Originally posted by John Meyers:
I ran the program and got the output 01234456. Try to run it again. Perhaps you forgot to give inputs through command line parameters ?
SCJP 5.0 (90%), SCDJWS 1.4 (88%), SCWCD 1.4 (82%), SCBCD 1.3 (85%)
SCJP 5.0 (90%), SCDJWS 1.4 (88%), SCWCD 1.4 (82%), SCBCD 1.3 (85%)
SCJP 5.0 (90%), SCDJWS 1.4 (88%), SCWCD 1.4 (82%), SCBCD 1.3 (85%)
Alexander Shrago wrote:There is the very strange situation.
The program prints nothing instead of expected answer (or it depends on types of shell and operation system - it very strange for java language).
Can someone explain the reason?
And can someone explain \d*+ regexp. Why it works correct instead of \d* ?
Watsh Rajneesh wrote:Again it will be interesting to know how the bash shell on linux behaves for this program. I dont have a linux setup so someone who has can give it a shot and share the result with us atleast for the following:
java Regex2 "\d*" ab34ef
where, cygwin bash shell prints nothing while win cmd prints 01234456.
Henry Wong wrote:
Alexander Shrago wrote:There is the very strange situation.
The program prints nothing instead of expected answer (or it depends on types of shell and operation system - it very strange for java language).
Can someone explain the reason?
And can someone explain \d*+ regexp. Why it works correct instead of \d* ?
First, you do know that this is a six year old topic, and that most of the ranchers in this conversation has likely moved on, right? Second, since the ranchers with the issues are not here, we can only speculate -- although, I am surprised that no one mentioned this six years ago.
With the bash shell (actually, with most of the unix shells), double quote do not prevent file globbing. The shell will do a file pattern match, and replace the pattern with the list of files that match. So.... I would speculate that the reason "\d*" didn't work was because the OP had a file in the current directory that started with the letter "d", and the shell replaced the regex pattern with a list of the matching files. I also speculate that the reason the patterns "\d?" and "\d*+" did work. was because the filenames was not two letters long, nor did it end with the plus sign -- when the shell fails to match, it keeps the original pattern.
And..... this has nothing to do with Java. File globbing is done prior to executing the command on the command line. The program (in this case, the JVM) don't see the original pattern when it has been replaced with the matching list of files.
Henry
gurpeet singh wrote:
i think the double quote characters( " " ) will escape any special character but not the $ sign on bash or any other linux shell.
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime. |