• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Locating Data via Pattern Matching

 
Ida Achi
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
With reference to K and B SCJP6 page 499. I would appreciate explanation why the answer is not

Pattern is \d\w
4 56
7 7ab

why was b ommited??





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

Produces the output

Pattern is \d\w
4 56
7 7a


Thanks
 
Henry Wong
author
Marshal
Pie
Posts: 21184
80
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

The pattern is a digit character followed by a word charater -- aka. two characters. How can the match return three characters, when the pattern is only looking for two characters?

Henry
 
Neha Daga
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
look carefully there is no quantifier so it will just look for a digit followed by any character and wiil give you that pattern only but if there was a '+' quantifier for '/w' then it would have returned everything after that unless there is a space or string has ended.
 
Ida Achi
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have no idea why I had imagined seeing a quantifier. Thanks for pointing that out.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic