| Author |
Regex splitting on carat symbol
|
Jeff Rahal
Greenhorn
Joined: Jun 01, 2005
Posts: 2
|
|
Hello all, I was given a file that is tilde-carat "~^" seperated that I need to parse into an array. The String.split(String regex) method seemed perfect, but when I put the carat in regex it does not work. It will split on s.split("~"), but not s.split("~^"). Usually this is resolved by escaping special regular expression characters, but when I make the regex "~\^" the compiler complains about an invalid escape sequence. Does anyone know how to handle such a situation? Thank you for your time.
|
 |
Ilja Preuss
author
Sheriff
Joined: Jul 11, 2001
Posts: 14112
|
|
|
You need to escape the backslash for the compiler, too: "~\\^"
|
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
|
 |
Ernest Friedman-Hill
author and iconoclast
Marshal
Joined: Jul 08, 2003
Posts: 24054
|
|
|
"^" is a special character in regexp, but not in a Java String, as you know. The problem is that both the regexp parser and the Java compiler both use "\" as an escape character. If you use a single backslash, then the Java compiler grabs it and the next character and tries to interpret them as an escape sequence. But you want the regexp parser to get the backslash, so you have to escape it so the Java compiler passes it along untouched -- i.e., your expression should be "~\\^", two backslashes instead of one.
|
[Jess in Action][AskingGoodQuestions]
|
 |
Jeff Rahal
Greenhorn
Joined: Jun 01, 2005
Posts: 2
|
|
|
Thank you very much for the quick replies!
|
 |
 |
|
|
subject: Regex splitting on carat symbol
|
|
|