permaculture playing cards*
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 | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
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: 1391
The string I am testing is:

FINISH IS^ SCRATCHED.

But the string remains unchanged.

Guido Granobles
Greenhorn

Joined: Sep 11, 2010
Posts: 22
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: 1391
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: 1391
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: 22
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://aspose.com/file-tools
 
subject: What is the trick to get ReplaceAll to Work
 
Similar Threads
which hirarcy methods will call
remove line feed and carriage return from a string
escaping html characters and a string starting with double quote
pre tag causes text not to appear?
Replace Carriage Returns in String