Win a copy of Terraform in Action this week in the Cloud forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Regex

 
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 anybody please explained why the answer, of below example, is
01234456 .
I know some body posted this same example, I went through that link. but still I am not getting it.

 
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
Sorry I forgot to put regex expression.

"\d*" "ab34ef"
 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Answer is
01234456
 
Nimit Shah
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
since \d* looks for digit and * is greedy quantifier. it looks for 0 or more occurances of digits.

a b 3 4 e f
Index 0 1 2 3 4 5

at index 0 there is occurance of 0 digit, so m.start() prints 0 as match found for 0 occurance of digit. and m.group() will print "" blank string.
same for b

then at index 2 it finds 34, so it prints m.start()=2 and m.group()=34.
case for e and f is same as a and b.

finally it will go after f i.e at index 6 it finds a match for 0 occurance of digit and m.start()=6 and m.group()=""

so, the final output is 01234456

I hope this is clear.
 
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Thanks for your clear explanation

but still i have a doubt why it has to check for 6th index when it is
empty/null.

Thanks
samura

 
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
Thanks Nimit for detail explanation. But Why it goes for index 6? Can you explain.
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was in doubt about this question too, so I did some compiling research. When I change the matcher to "" (instead of "ab34ef"), the result is "0".

My conclusion about this: the matcher recognizes a last "" character.
 
Rancher
Posts: 43027
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The question gets asked very frequently. Searching through either the discussion archives or the FAQ for "Regex2" would have pointed you to the lengthy discussion in the SCJP FAQ.
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Howdy, Paul!

Seems to be your first posting here, so


Welcome to the Ranch!



Your conclusion is correct, look out for the term "zero string".
Perhaps you can even make a search (see panel right from "moosy") with just that famous (notorious...) String "ab34ef".


Yours,
Bu.
 
Nimit Shah
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry that I couldn't post the reply after initial explanation.

I was busy in preperation , just cleared the exam with 98%

Coming to question why it gives blank String at index 6 for "ab34ef" while index goes from only 0-5 in given String.

Answer:
If we take a String with just 1 character then start index is 0 and endindex is 1, there is no characted at index 1.

String "s"
| |
index: 0 1

So, in case of String.substring(0,1) it gives "s" which indicates substring with start index of 0 and end index of 1 and exclude end index as there no character at end index.
Now, when you apply a greedy quantifier * (0 or more times). It will consume greedily. Since the end index is there without any character it fits in the search criteria.

I hope this is clear now.
 
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
Congrats for a great score.
Thanks for reply.
Can you give me some tips? I am planning to give exam in a month.
 
Nimit Shah
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I prepared from K & B book.

Got good help from Java ranch and javabeat yahoo group.
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello All,

I was working on Regex and found the same code which is in context in K&B book . But to my surprise, the invocation was like

java Regex "\d*" ab34ef , where as I was expecting java Regex \d* ab34ef

As far as I know, all command line arguements are Strings, so they are just good to be a Pattern and Matcher arguements. But why was \d* to be coded explicitly in "".

Also I tried to run removing the quotes around \d*, believing still it is a string. but nothing displayed. What could have went wrong?

Can anyone please explain me, the need of quotes around \d*?
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic