开发者

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜