• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Regex Doubt

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
You ridiculous clown, did you think you could get away with it? This is my favorite tiny ad!
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic