trouble using | (pipe) with the String.split method
I came across this little problem whilst coding this week and it was driving me crazy and it is only a little problem so I wasn't going to blog about it but I couldn't find any information on it anywhere so I thought I would write a blog entry in case someone else has this problem.
The problem came when I tried to using String.split method, you replace String with an actual String and not the static object. Anyway I had written a log file and used | (pipe) as a delimeter so that when I got the log file in my code I could just do a split("|") and then it would chop each line into a nice array of Strings which I could loop through and search and take out the values I wanted.
The problem I was getting was when I did split
myString.split("|")
it split the string on every single character, which was quite a shock. I tried to goggle the problem by typing in things like
"using pipes with split()"
"pipe metacharacters"
I knew from my study of the SCJP 5 that there is some tricky characters in regex (regular expressions) called metacharacters but I couldn't find | included in any of them and wasn't sure why it was acting funny. I have blogged about metacharacters before in a blog entry called
\n - newline saves the day and formats all my troubles away
In the end I found that to successfully use the pipe in a regular expression I had to add a double backslash like so
myString.split("\\|");
and then it worked fine.
I thought I would blog about this just in case someone else runs across this problem. I'm not sure perhaps its something which everyone knows not to do (except me obviously) , I know pipes are used for adding things together but I still don't really understand why it would split on everyone single space
The problem came when I tried to using String.split method, you replace String with an actual String and not the static object. Anyway I had written a log file and used | (pipe) as a delimeter so that when I got the log file in my code I could just do a split("|") and then it would chop each line into a nice array of Strings which I could loop through and search and take out the values I wanted.
The problem I was getting was when I did split
myString.split("|")
it split the string on every single character, which was quite a shock. I tried to goggle the problem by typing in things like
"using pipes with split()"
"pipe metacharacters"
I knew from my study of the SCJP 5 that there is some tricky characters in regex (regular expressions) called metacharacters but I couldn't find | included in any of them and wasn't sure why it was acting funny. I have blogged about metacharacters before in a blog entry called
\n - newline saves the day and formats all my troubles away
In the end I found that to successfully use the pipe in a regular expression I had to add a double backslash like so
myString.split("\\|");
and then it worked fine.
I thought I would blog about this just in case someone else runs across this problem. I'm not sure perhaps its something which everyone knows not to do (except me obviously) , I know pipes are used for adding things together but I still don't really understand why it would split on everyone single space
94 Comments:
The pipe is the alternation character. Just having:
myString.split("|");
means "split on null or null." Or in Java's case, "split on every character."
By Unknown, at Thu Nov 30, 07:41:00 pm 2006
The pipe symbol ("alternation") is used to match the expressions it separates. The String.split() method splits on either side of the pattern match, consuming the value that was matched:
"X comes before Y which is before Z".split("X|Y|Z")
will return an array:
{"", " comes before ", " which is before "}
Calling split("|"), is logically the same as split(""). That's basically saying split on either side of nothing. Thus the behavior you observed:
"X comes before Y which is before Z".split("|")
will return an array:
{"", "X", " ", "c", "o", "m", "e", "s", " ", "b", "e", "f", "o", "r", "e", " ", "Y", " ", "w", "h", "i", "c", "h", " ", "i", "s", " ", "b", "e", "f", "o", "r", "e", " ", "Z"}
This is a bit tricky, just try to remember you frequently need to escape meta-characters when you intend to use them as literals. Failure to do so can produce some surprising results!
By Anonymous, at Thu Nov 30, 07:47:00 pm 2006
You might find this little method useful to prevent such problems in the future:
http://java.sun.com/j2se/1.5.0/docs/api/java/util/regex/Pattern.html#quote(java.lang.String)
By Ricky Clarkson, at Thu Nov 30, 10:31:00 pm 2006
I was having the same problem and you helped me out. Thanks.
By Anonymous, at Fri Dec 29, 09:46:00 pm 2006
You helped me too ...
Thanks
By Anonymous, at Wed Oct 17, 02:44:00 am 2007
It helped me
By Anonymous, at Fri Dec 14, 10:32:00 am 2007
Keep it up.... u jst helped another guy...
thnxxx....
By Anonymous, at Thu Feb 07, 04:12:00 am 2008
You can use "abc".split("[|]"). This works correctly
By Anonymous, at Tue Apr 15, 10:47:00 am 2008
very useful...thanks a ton :)
-wyrmmage
By Anonymous, at Fri Jun 13, 05:53:00 pm 2008
was banging my head... thanks!
-dave
By Anonymous, at Fri Jul 25, 06:23:00 pm 2008
Good post. You saved my time :)
By Anonymous, at Wed Aug 13, 10:46:00 am 2008
I had the same problem. Thanks for the write up and the comments explaining the issue.
By Anonymous, at Fri Aug 22, 10:09:00 pm 2008
I find one more problem. Actually I have a pipe "|" separated string and if value is null then there is nothing between pipes(||); The problem is if in the last of string there is no character except pipes, then it ignore all pipes.
Suppose String name="a|b|c|d||||||"
if we write String[] values=name.split("[|]") it gives only four element(a,b,c,d) in array. If I put a character in the last of the | seperated string it gives all(some null value in array). While StringUtils class of apache works fine.
By Anonymous, at Fri Sept 12, 04:35:00 pm 2008
Thanks for writing this....
This really helped..
By RBS, at Sat Oct 18, 08:10:00 am 2008
Thx man, I was facing the same problem!
By Flavio Furlanetto, at Wed Oct 22, 02:46:00 pm 2008
Thanks, I was going through the same problem!
By Anonymous, at Fri Oct 24, 11:43:00 am 2008
thanks you're a lifesaver!
By Anonymous, at Wed Nov 05, 09:23:00 pm 2008
Thanks so much for your blog! You saved me tons of time and frustration!
By Anonymous, at Fri Nov 07, 08:40:00 pm 2008
Thank you so much .. also for the explanation .. it was driving me to the wall...
By Xac, at Wed Nov 12, 05:37:00 pm 2008
Much appreciated.
By Anonymous, at Tue Nov 18, 04:22:00 pm 2008
thank you sooooooooo much jaja
this thing was driving me crazy
thanks again
By Anonymous, at Mon Nov 24, 05:18:00 am 2008
Thanks for posting this!
By Ravi, at Mon Dec 22, 06:07:00 am 2008
Thanks, couldn't work it out either!!
By Unknown, at Wed Jan 21, 02:16:00 pm 2009
Thanks for posting this.
Much appreciated.
By Anonymous, at Mon Feb 02, 09:00:00 pm 2009
Thanks a lot for posting this, just saved me a lot of googling.
By AndresBMorales, at Fri Feb 06, 01:29:00 am 2009
Thank you for posting this, and thank you to the people posting follow-up comments.
By Mitama, at Wed Feb 18, 05:08:00 pm 2009
Thanks a lot for posting this - you saved the day for someone else!
By wampum, at Wed Mar 25, 08:55:00 pm 2009
Thanks a lot!
By Anonymous, at Mon Mar 30, 02:55:00 am 2009
I ran exactly into this problem. Your post helped me much, tnx!
By Inwit, at Mon Apr 06, 02:02:00 pm 2009
Another grateful reader !
Thanks a lot
By Anonymous, at Mon Apr 13, 12:31:00 pm 2009
Thx dude, seems like a couple of years later your post still remains helpful!
By luke, at Wed Apr 22, 09:02:00 pm 2009
10x!
By Anonymous, at Tue Apr 28, 09:13:00 am 2009
Thanks ! Helps me too
By Jason, at Mon May 04, 09:05:00 pm 2009
I ran into this aswell.
I remembered when i saw "pipe metacharacters" in here. It's a special char and it has to be escaped using "\\". Just like
\t ! # $ % ' ( ) * + , - . / : ; = ? @ [ \ ] ^ _ ` { } ~
Thankyou for reminding me that.
By Anonymous, at Thu May 07, 12:29:00 pm 2009
Thanks!!! Keep on blogging however silly you think it is...
By Anonymous, at Mon May 11, 08:52:00 pm 2009
Thx.
By Bill Comer, at Tue May 12, 05:08:00 pm 2009
Thank you; I had the same problem, thought about escaping it with a single or double backslash, but did not do it until I saw your post. Very helpful.
By Anonymous, at Thu May 14, 10:01:00 pm 2009
Thanks..it Helps
By Unknown, at Mon May 25, 06:28:00 pm 2009
Thank you, this rocks!
By Unknown, at Fri Jun 05, 10:22:00 pm 2009
Thanks a million!
By Anonymous, at Fri Jun 12, 02:23:00 pm 2009
Thanks!
By Anonymous, at Wed Jun 24, 04:17:00 am 2009
Thanks, cleared it up for me!
By Anonymous, at Fri Jun 26, 03:17:00 pm 2009
it helped a lot
By Anonymous, at Fri Jul 03, 07:30:00 am 2009
Thank you!!!
By Anonymous, at Tue Jul 07, 05:26:00 pm 2009
thanks
By Mohamed Shaheen, at Tue Jul 21, 09:34:00 am 2009
Thanks a lot
I never observed
Came to know whn i tries in my prog
By Anonymous, at Thu Jul 30, 11:51:00 am 2009
Thank you, helpful information.
By Anonymous, at Sun Aug 09, 07:33:00 am 2009
Thx for the help.
By Anonymous, at Thu Aug 13, 05:04:00 pm 2009
Thank you! This entry was very helpful for me. That's what what I was looking for.
By Anonymous, at Mon Sept 14, 02:30:00 pm 2009
Thank you!!! Your post was helpful for me
By Anonymous, at Thu Sept 17, 03:54:00 pm 2009
Thanks a lot for saving another guy's day.
By Anonymous, at Mon Sept 21, 10:34:00 am 2009
Thanks, helped me too.
By Anonymous, at Thu Sept 24, 03:45:00 am 2009
Thanks very very much friend. I was struck and your post helped me a lot. Once again thanks for posting!!!
By Anonymous, at Thu Oct 01, 06:25:00 pm 2009
Thanks a lot, this was driving me crazy!
Thanks to the guys who developped the application using | as delimiters I've got a serious headache ... Thanks to this post its relieved (a bit already) ;-)
By Anonymous, at Tue Oct 06, 01:51:00 pm 2009
Thanks, this really helps!
By Anonymous, at Thu Oct 15, 02:12:00 pm 2009
Your post saved me from reinventing the wheel. Thanks.
By Anonymous, at Wed Oct 21, 04:25:00 pm 2009
You saved me a lot of time my friend :)
By Anonymous, at Tue Oct 27, 03:46:00 am 2009
OMG This had been driving me crazy for hours! Thank you so much!!!
By G, at Sun Nov 15, 06:40:00 pm 2009
My boss told me that, since I was using the pipe as separator, I had to escape it in the regular expression:
\|
But then, I had to escape the \ because its java, hence:
\\|
By Jro., at Fri Nov 27, 02:27:00 pm 2009
Thank you for taking the trouble to write it down. You saved my day..
When you search with "java regex split from pipeline", the first result is your page on google by the way.
By Elif, at Mon Dec 21, 12:05:00 pm 2009
Seems a popular post even years later!
It's worth noting that the double-slash is only necessary if the expression string is compiled into your java code.
One slash is needed by the Java *compiler* so that it stores a literal slash in the expression String object. This isn't unique to regular expressions, all Java Strings compiled in the class files work this way. If you want one slash in your String in your compiled class file, type two slashes in your Java source.
If the regular expression is passed into the Java program at runtime -- via command-line arguments, or a property value -- this first slash is not necessary.
Another slash is needed by the regular expression parser so that the pipe is treated as a literal character. The expression parser doesn't care whether the string was compiled into the class file or provided at runtime.
Source code: \\|
... compiles to...
Class file: \|
... is read by regular expression parser as...
Expression parser treats as: |
By christophergraz, at Mon Jan 04, 10:56:00 pm 2010
We love you! :)
By Gergely Bacso, at Wed Feb 03, 12:14:00 pm 2010
Heyy I was facing the same problem..Thanks!!
By sony26, at Tue Feb 23, 08:53:00 pm 2010
Thanks a lot...
This alsouseful http://java.sun.com/j2se/1.5.0/docs/api/java/util/regex/Pattern.html#quote(java.lang.String)
Suggested by Ricky
By Vishawjeet Saini, at Wed Mar 03, 11:55:00 am 2010
thanks.
By TheKing, at Fri Mar 26, 03:40:00 am 2010
:O How many people having the same simple problem!!!
I was one of them! :/
Thanks!!
By Livia Barcelos, at Wed May 26, 07:11:00 pm 2010
Thanks it helped
By Unknown, at Wed Jun 23, 10:08:00 am 2010
I am the latest one in the thanking list :-)
Thanks !!
By Unknown, at Mon Aug 09, 12:04:00 am 2010
Thanks for this!
By Arvind, at Thu Aug 26, 08:38:00 am 2010
Thanks a lot man.. you saved my day...
By Jaykiran, at Tue Nov 09, 08:48:00 pm 2010
I'll join in the flood of thanks. Four years and this tip is still helping people. Cheers.
By timbonicus, at Tue Feb 15, 10:27:00 pm 2011
Thanks. It helped me a lot.
By Hiren Patel, at Thu Mar 24, 06:54:00 pm 2011
Thanks, it was very helpful
By mostafa hashem, at Tue Jun 07, 02:08:00 am 2011
Hey, I just wanted to say thanks for the post; I've been frustrated for close to an hour now. Thanks!
By Michael, at Wed Jun 15, 01:39:00 am 2011
thank you all for solution as well as reason
By lost_myself_somewhere, at Fri Jun 24, 06:36:00 am 2011
;) thks!
By Anonimo, at Tue Jun 28, 03:51:00 pm 2011
Thanks for the posting , really it saved my time.
By Rajendra Prasad, at Mon Jul 25, 11:36:00 am 2011
thank you so much. It helps a lot.
By PDA Vegeta, at Wed Sept 14, 09:02:00 pm 2011
Excellent, quick and well explained solution. Thanks!
By Anonymous, at Tue Oct 25, 03:53:00 pm 2011
Thanks!
By TimoTaye, at Tue Nov 22, 07:16:00 pm 2011
thanks and keep it up
By Prashant Devda, at Wed Nov 30, 02:12:00 pm 2011
Thank you, solved my problem!
Best regards
By Rubén, at Fri Dec 23, 12:19:00 pm 2011
thanks !
By vali, at Thu Jan 12, 10:31:00 am 2012
Java blows. I've spent a while troubleshooting the same absurd problem. And I think after a couple decades they just released a decent "read file" function. /venting
By Unknown, at Wed May 02, 03:19:00 pm 2012
7 years later, this still helps. As others have said, thanks for the solution.
By ace danger, at Mon May 07, 03:16:00 pm 2012
Still Helping people.. Just resolved my issue. Thanks!
By Epikto, at Thu Jul 05, 04:22:00 am 2012
Chock one more grateful recipient on the list.
By iroh, at Thu Sept 06, 09:07:00 pm 2012
Thank you very much for saving our time :) :) a hug :)
By Kathik, at Fri Sept 28, 08:07:00 am 2012
Thanks from Brazil :)
By Andy, at Wed Nov 21, 04:44:00 pm 2012
OMG Thank, Saved Me So Much Time :) Thanx From Sri Lanka!
By Code Gears Team, at Sun Dec 23, 12:49:00 am 2012
omg thanks for this !
By sasklacz, at Thu Jan 10, 02:43:00 am 2013
omg thanks for this !
By sasklacz, at Thu Jan 10, 02:43:00 am 2013
Hey man, worked like a charm. Thanks for clearing this up with ease!
By Nate, at Wed Feb 13, 10:08:00 pm 2013
Thankyou from the University of Nairobi, Kenya
By DudusBlack, at Wed Feb 12, 01:27:00 pm 2014
Post a Comment
<< Home