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

Possessive Quntifiers

 
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can any one please explain the output of the following program:



The output is "No match".-- I don't understand
 
author
Posts: 23919
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A possessive qualifier is simply a greedy qualifier that won't backtrack -- won't return what it (sub) matched.



In this example, the ".*+" matched everything from after the first "ab" all the way to the end of the string. And since it won't backtrack, there is nothing left to match the last two "ba" in the pattern -- hence, no match.

Henry
 
Dean Jones
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Henry,does that mean,whenever we want to search a sequence of more than one character, possessive quantiers will always provide a no match. Also one more doubt,why do we get an IllegalStateException?
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why do we get IllegalStateException??

>>MatchResult result = s.match();

match() method throws IllegalStateException if no match is available. To avoid this scenario use various next() methods of scanner to find match.
 
Henry Wong
author
Posts: 23919
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Dean Jones:
Thanks Henry,does that mean,whenever we want to search a sequence of more than one character, possessive quantiers will always provide a no match.



No... it just means that you shouldn't be using a pattern that can match anything (such as the "."). It also means that you have special needs to have backtracking disabled -- a situation that I personally have never needed.

Henry
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[Dean]: Why do we get IllegalStateException??

[Sara]:

>>MatchResult result = s.match();

match() method throws IllegalStateException if no match is available. To avoid this scenario use various next() methods of scanner to find match.


In this case, the method s.findInLine() was already called. This will make a match available if there is one in the current line. It also returns a String of the matched region. If there's no match, it returns null. So rather than using the try/catch block above, it would make more sense to check the return value of findInLine(). If it's null, don't bother trying to call match(), as it will throw an exception.

Incidentally, for the current pattern, there's really no need to bother with match() at all. The String returned by findInLine() has all the information you need. The match() would be useful if you cared about the start() and end() of the match, or if you had any interesting groups to look at. But the String returned by findInLine() is already the same as group 0, the entire match. And your regex doesn't have any other groups within it - no matched parentheses. So the MatchResult object really isn't going to tell you anything you couldn't get from the String in this case. However if you experiment more with different regexes, it could be useful to show all the matched groups, as you are doing.
 
I suggest huckleberry pie. But the only thing on the gluten free menu is this tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic