Replace a character squence in java using regular expression
I have the following text
"This ball isn?t yours, this one is John?s"
I want to correct this to be
"This ball is开发者_Go百科n't yours, this one is John's"
How can I do this in Java using Pattern and Matcher?
string.replaceall
String fixed = old.replaceAll("\\?([ts])", "'$1");
Here's an example
In this case you could use:
s = s.replaceAll("\\b?\\b", "'");
Then you'll be much less likely to replace legitimate question marks, as @glowcoder mentioned. However, I think @Philipp is right, and this is really a character-encoding issue. It looks like your text was supposed to be:
"This ball isn’t yours, this one is John’s"
If it was encoded as cp-1252 but decoded as ASCII, the curly single-quotes would be replaced with question marks. If that's the case, you're likely to find other characters, like curly double-quotes (“ ”
), en-dash (–
) and em-dash (—
), that have been munged in the same way.
精彩评论