aspose file tools*
The moose likes Beginning Java and the fly likes split and regexps Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "split and regexps" Watch "split and regexps" New topic
Author

split and regexps

jeroen dijkmeijer
Ranch Hand

Joined: Sep 26, 2003
Posts: 131
Hello to all,

I have some code which has some perl like behavior and allows me to split a line with some separators onto a head and tail specifying an index. A negative index will start from the end of the string and will make the tail the head, handy if you're not sure on the number of columns in your list.


All works beautiful and as expected however, the split uses regexes to match the column separators, works for ~ but not for "|", which is treated as a regex special character, I may be able to pass in "\\|" but that upsets the join feature.
So my question is: is there a way to use split only on normal "string tokens" or are there other clever ways to get divide a string in the documented way.

Thanks in advance.
jeroen.
My question is there a way to make the split none regex like
Alan Moore
Ranch Hand

Joined: May 06, 2004
Posts: 262

Or, if you're running JDK 5 or later,
jeroen dijkmeijer
Ranch Hand

Joined: Sep 26, 2003
Posts: 131
Aah the quote functionality was something I was looking for!
good thing they put it in 5, too bad I'm bound to 1.4, I'll stick to the replace all functionality. Thanks for the lesson!
Jeroen.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
In JDK 1.4, you can also use

String regex = "\\Q" + separator + "\\E";

That's actually what the later Pattern.quote() uses (unless the regex itself contains \\E internally).


"I'm not back." - Bill Harding, Twister
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18829
    
  40

Here is a JDK 1.4 version of a quote() method -- that functionally does the same thing as the JDK 1.5 version of the Pattern.quote() method.



Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Alan Moore
Ranch Hand

Joined: May 06, 2004
Posts: 262
The \Q...\E thing is a carryover from Perl; there's no good reason to use it in Java, considering how much more complicated it is to work with. I've never understood why they used it in the quote() method, but at least we don't have to deal with it.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: split and regexps