• 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

String.split() lazy or greedy

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

I am not sure what is happening in this example:



This example prints 8, not 4 as I would have imagined.

This would imply that split is lazy. Am I correct? Where can I find this in the API doc?

Thanks ranchers,

Cory
[ December 16, 2007: Message edited by: Cory Max ]
 
author
Posts: 23951
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

This would imply that split is lazy. Am I correct? Where can I find this in the API doc?



The split() method is neither "reluctant" or "greedy" -- in fact, it is debateable if a regex is "reluctant" or "greedy".

It is the qualifiers in the regex, that can be classified as "greedy" or not -- and you don't have any qualifiers in your regex.

Henry
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Cory Max:



If you add this code:

you will see that indeed there are 8 strings, each separated by a digit as delimiter, but only 4 strings that include a character. K&B page 486 has a good explanation.
 
reply
    Bookmark Topic Watch Topic
  • New Topic