Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

regular expression with special character

 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34973
379
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does anyone know why these two code snippets output different things? The first outputs "tes" which is what I would expect. The second outputs "test" which implies the regular expression doesn't match.


According to the JavaDoc for the String class, these are supposed to be identical. Am I doing something wrong when transposing?

They both work correctly if I remove the $. Then both output "es".
 
Henry Wong
author
Marshal
Pie
Posts: 21500
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
According to the JavaDoc for the String class, these are supposed to be identical. Am I doing something wrong when transposing?


You are calling the wrong method. You should be calling the replaceAll() method. The replace() method doesn't take a regex.

Henry
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34973
379
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Henry Wong:
You are calling the wrong method. You should be calling the replaceAll() method. The replace() method doesn't take a regex.

Henry,
A 3 minute response time. Very impressive!

And - doh! I thought "replaceAll". I even looked in the JavaDoc under "replaceAll" to see if it was defaulting to different pattern flags. I've used this method numerous times (correctly.) And yet staring at it - nothing.

Thanks.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic