• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Mock -Example Regex metacharacter

 
Ranch Hand
Posts: 278
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 278
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 278
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 383
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please refer below link.

Link
 
Lucky J Verma
Ranch Hand
Posts: 278
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ]
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 383
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you post your code? It helps to everybody to answer.
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 278
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 278
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For me ,
with \d+ same output is coming .
but with \d*, no output is coming.
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
The happiness of your life depends upon the quality of your thoughts -Marcus Aurelius ... think about this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic