• 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
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Regex - Greedy *

 
Ranch Hand
Posts: 430
Android VI Editor Debian
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How did the regex engine work this one out



Output



I was expecting the output to be "string two" because the greedy * would have resulted in it searching from the right?
 
Greenhorn
Posts: 13
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not necessarily from right. It depends on your pattern.

Use ".*\"" and you will get 0 : 61 : Houston, we have a problem with "string one" and "string two" (Chopping from right)

Use "\".*" and you will get 32 : 78 : "string one" and "string two". Please respond. (Chopping from left)


In your case, source string is chopped from both ends. So you get 32 : 61 : "string one" and "string two"

Chopping is also called backing off and can occur from any end of source.


Rule of thumb : "Greedy quantifier returns the biggest possible search result."


 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic