Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

What is the trick to get ReplaceAll to Work

 
Steve Dyke
Ranch Hand
Posts: 1619
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The string I am testing is:

FINISH IS^ SCRATCHED.

But the string remains unchanged.

 
Guido Granobles
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What are you trying to do?. Split the line ?
 
Lester Burnham
Rancher
Posts: 1337
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"^" 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
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The first argument in replaceAll is a regex pattern and ^ is a special character in regex. You should escape it.
 
Steve Dyke
Ranch Hand
Posts: 1619
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1619
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic