简体   繁体   中英

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 isn'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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM