aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Mock -Example Regex metacharacter Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Mock -Example Regex metacharacter" Watch "Mock -Example Regex metacharacter" New topic
Author

Mock -Example Regex metacharacter

Lucky J Verma
Ranch Hand

Joined: Apr 11, 2007
Posts: 278
In K & B Book ,Chap 6 page ,6
1Ques,
In Regex, its giving ,java Regex2 "\d" ab34ef

But i read for \d we have to use \\d as ,compiler thinks \d is some escape sequence.
but here ,on command line ,dont we reqire 1 more slash.
"\\d"
Lucky J Verma
Ranch Hand

Joined: Apr 11, 2007
Posts: 278
can anybody help me explaining this code and its output
for me ,its coming nothing.
with \d (no *) -> 2334 is coming .
how comes answer is E
Lucky J Verma
Ranch Hand

Joined: Apr 11, 2007
Posts: 278
Its some error in answer.
It is requested to Authors to please correct it,and lead us to correct explanation for this question .
As
Answer on page 508 in chap 6 ,K &B says
Correct answer E
Incorrect answer : A,B,C,D,E,F,G
dolly shah
Ranch Hand

Joined: Jun 18, 2007
Posts: 383
Please refer below link.

Link


SCJP-1.5<br />SCWCD-1.4
Lucky J Verma
Ranch Hand

Joined: Apr 11, 2007
Posts: 278
Hi Dolly
Thanks for the link .It was really helpful.
But my code is still not running .
I tried all commandline combinations with quotes
\d* ab35ef
with and without quotes,Nothing is coming .
but if i change quantifier ,\d+ some output showing ,with or without quotes even.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Lucky J Verma:
In K & B Book ,Chap 6 page ,6...

I think that's page 498, for anyone following.

I get the expected output, whether I use 1 backslash or 2.

> java Regex2 "\d*" ab34ef
> 01234456

> java Regex2 "\\d*" ab34ef
> 01234456

Can you post the exact code you're using?

Also, have you seen our FAQ: Could someone explain the Regex2 class from K&B's Java 5 Study Guide?
[ August 30, 2007: Message edited by: marc weber ]

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Lucky J Verma:
... But i read for \d we have to use \\d as ,compiler thinks \d is some escape sequence.
but here ,on command line ,dont we reqire 1 more slash. "\\d"

The escape sequences for Java Strings are listed under JLS - 3.10.6 Escape Sequences for Character and String Literals.

For example, in a String literal, \b is an escape sequence interpreted as a backspace. However, in a regex Pattern, \b represents a word boundary. So to prevent \b from being interpreted as a backspace in a regex Pattern, an additional escape is required: \\b.

However, note that \d is not listed as an escape for a String literal. So when used to represent any digit in a regex Pattern, it does not require an additional escape.

(Also see java.util.regex.Pattern in the API.)
dolly shah
Ranch Hand

Joined: Jun 18, 2007
Posts: 383
Can you post your code? It helps to everybody to answer.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by dolly shah:
Can you post your code? It helps to everybody to answer.

It's posted in the FAQ item I linked to above. But here it is again...
Lucky J Verma
Ranch Hand

Joined: Apr 11, 2007
Posts: 278
Hi ,

I am posting my code.
It is same code from K & B Book .For everyone ,as i read aswer is 1234456.but for me no output is coming.
i passed command line arguments "\d*" ab12ef
i have tried all combination sinlge slash double slash ,qutoes no quotes.
but if i change * to + like \d+ its working .* is not working for me.
I am surprised ,as i m trying for whole day.
<code>
import java.util.regex.*;

public class RegExDemo2 {
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.println(m.start()+"-"+m.group());
}


}}
</code>
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Here is my output using your code with Java 1.5.0_06.

...>java RegExDemo2 "\d*" ab12ef
0-
1-
2-12
4-
5-
6-

...>java RegExDemo2 "\d+" ab12ef
2-12

Do you get any output at all using "\d*" ab12ef?
Lucky J Verma
Ranch Hand

Joined: Apr 11, 2007
Posts: 278
For me ,
with \d+ same output is coming .
but with \d*, no output is coming.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

What version of Java are you using?

(I've read about purported regex bugs in 1.6.)
[ August 31, 2007: Message edited by: marc weber ]
 
 
subject: Mock -Example Regex metacharacter