• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
  • Paul Clapham
Sheriffs:
  • paul wheaton
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Piet Souris
Bartenders:
  • Mike London

SCJP 6 K&B book page no: 499

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


% java Regex "\d\w" "ab4 56_7ab"


Produces output:

patter is \d\w
4 56
7 7a



Can anyone explain how can i get that output?? \d\w. What i understood is it should search for a digit follwed by a char. but how is that output coming?

Please help.

Thanks,
Madhavee Latha


 
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well the first part of output in the first line is the start index of the pattern.As you know the index starts from 0 so it starts its searches from 0 and the first digit it encounters is 5 and then it should be followed by word character which could be [a-zA-Z0-9_] so 56 is the first pattern that matches adn the index of 5 is 4 hence the output is 4 starting position and the matched string 56.

4 inthe output is not the 4 from the input String.

Hope it helps
 
Madi Meka
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ya i understand that 4 and 7 are the indexes. but couldn't frame 56 as i thought it should result in 56_7ab for \d\w(digit followed by a string).
 
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Can anyone explain how can i get that output?? \d\w. What i understood is it should search for a digit follwed by a char. but how is that output coming?



You get \d\w in the output because you have instructed the code to do so. Please see Line 1 here you are displaying the regex pattern being used.



 
Siva Masilamani
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
/d/w means one digit followed by one letter

/w means a word character not a word.Word character could be a-zA-Z0-9 or_

So as per your input string the first number is 4 but it should be followed by word character but space is not word character and hence no match and regex move on to fins next number here it finds 5 then it finds 6 followed by it .since 6 is word character followed bu digit 5 it matches the pattern and hence the output and continues.......
 
Madi Meka
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ohh okk... Thanks a lot.
reply
    Bookmark Topic Watch Topic
  • New Topic