aspose file tools
The moose likes Java in General and the fly likes Regex splitting on carat symbol Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Regex splitting on carat symbol" Watch "Regex splitting on carat symbol" New topic
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
    
  13

"^" 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!
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Regex splitting on carat symbol
 
Similar Threads
Discussing errata for K&B, SCJP 6
split()
WA #1.....word association
Split a string and add the contents
Regular Expressions: A String should not contain the word "TEST"