aspose file tools
The moose likes Java in General and the fly likes What is the trick to get ReplaceAll to Work Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "What is the trick to get ReplaceAll to Work" Watch "What is the trick to get ReplaceAll to Work" New topic
Author

What is the trick to get ReplaceAll to Work

Steve Dyke
Ranch Hand

Joined: Nov 16, 2004
Posts: 1261
The string I am testing is:

FINISH IS^ SCRATCHED.

But the string remains unchanged.

Guido Granobles
Greenhorn

Joined: Sep 11, 2010
Posts: 21
What are you trying to do?. Split the line ?


http://guido-granobles.blogspot.com
Lester Burnham
Rancher

Joined: Oct 14, 2008
Posts: 1337
"^" is a special character when it comes to regular expressions (which is what replaceAll does) - it needs to be escaped: try "\\^" instead.
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

The first argument in replaceAll is a regex pattern and ^ is a special character in regex. You should escape it.


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Steve Dyke
Ranch Hand

Joined: Nov 16, 2004
Posts: 1261
Guido Granobles wrote:What are you trying to do?. Split the line ?


No. When the record is created the user inputs the data into a textarea field. This allows them to use carriage returns and line feeds. Before the data is stored in the database, since the characters used to do the carriage return and line feed is difficult to store I replace these with the '^' character. Now when the data is pulled to be displayed I need to put the carriage returns and line feeds back in where ever a '^' occurs.
Steve Dyke
Ranch Hand

Joined: Nov 16, 2004
Posts: 1261
Lester Burnham wrote:"^" is a special character when it comes to regular expressions (which is what replaceAll does) - it needs to be escaped: try "\\^" instead.


Thanks again. Works great.
Guido Granobles
Greenhorn

Joined: Sep 11, 2010
Posts: 21
That make sense. Althought there is a blank space between ^ and SCRATCHED. So you will have like a TAB in the next line.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

How are CRLFs difficult to store?! Storing them is no different than storing any other character. And what if there's a caret in the text?
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: What is the trick to get ReplaceAll to Work
 
Similar Threads
pre tag causes text not to appear?
which hirarcy methods will call
Replace Carriage Returns in String
remove line feed and carriage return from a string
escaping html characters and a string starting with double quote