• 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

Java Regular Expressions

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Given a String that looks like this:
String temp = "\t\tAB\t\t";
I expect that when I do this:
String[] cols = temp.split("\t");
I would get a String array of size 5, each element in the array looking like this:
cols[0] == ""
cols[1] == ""
cols[2] == "AB"
cols[3] == ""
cols[4] == ""
But I don't! Instead I get this:
cols[0] == ""
cols[1] == ""
cols[2] == "AB"
What happened to the splits on tabs that occurred after the character sequence AB?
When I split I want the empty Strings before and after EVERY tab in the original String. I don't want to capture any more than this. When I try using boundary matchers in my regex I capture too much.
It seems so easy. I simply want to split on each and every tab. That's it! Any advice would be MUCH appreciated!
Thanks a bunch in advance,
Patrick Garner
[ January 11, 2004: Message edited by: Patrick Garner ]
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Take a look at the API for split(String, int) in the String class. It turns out that you can control the behavior of split() using this int parameter, depending on whether it's positive, zero, or negative. For your purposes, you want

Or more generally, any negative number will work the same as -1 here. This rather bizarre API was evidently based on Perl's split function, which also can accept a second (int) parameter which is interpreted in the same way.
 
Patrick Garner
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you, thank you, thank you!!!
 
reply
    Bookmark Topic Watch Topic
  • New Topic