• 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

regular expression problem

 
Ranch Hand
Posts: 924
1
Netbeans IDE Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
please refer the javadocs given at http://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html#split(java.lang.CharSequence, int)


public String[] split(CharSequence input)
Splits the given input sequence around matches of this pattern.
This method works as if by invoking the two-argument split method with the given input sequence and a limit argument of zero. Trailing empty strings are therefore not included in the resulting array.

The input "boo:and:foo", for example, yields the following results with these expressions:

Regex

Result

: { "boo", "and", "foo" }
o { "b", "", ":and:f" }




i'm not able to understand the second output i.e. when the regex is o. can somebody explain me how to get the output ?
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

gurpeet singh wrote:please refer the javadocs given at http://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html#split(java.lang.CharSequence, int)


The input "boo:and:foo", for example, yields the following results with these expressions:

Regex Result

o { "b", "", ":and:f" }



i'm not able to understand the second output i.e. when the regex is o. can somebody explain me how to get the output ?



It's going to split the string at all instances of "o". It will also remove all trailing instances of "o". So, immediately, your string will become "boo:and:f".
Working from this, at all instances of "o", your string will be split. In cases of consecutive o's, empty string ("") will be used. So your string is split like this:

"boo:and:f" => "b" + "" + ":and:f". All of these pieces will be put into an array like the result quoted above.
 
Ranch Hand
Posts: 451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
boo:and:foo is splited by o.
So, you will get b, "", ":and:f" and "".

Why do you get ""?
Because when oo is splited by o, it leaves you a zero length string "".

 
gurpeet singh
Ranch Hand
Posts: 924
1
Netbeans IDE Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Brian and helen. was desperately waiting for someone to answer.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic