简体   繁体   English

使用正则表达式替换Java中的字符顺序

[英]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? 如何使用Pattern和Matcher在Java中做到这一点?

string.replaceall 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. 然后,您将不太可能替换合法的问号,如@glowcoder所述。 However, I think @Philipp is right, and this is really a character-encoding issue. 但是,我认为@Philipp是正确的,这确实是一个字符编码问题。 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. 如果将其编码为cp-1252但解码为ASCII,则卷曲的单引号将替换为问号。 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. 如果是这种情况,您很可能会发现其他字符,例如以相同的方式修饰的卷曲双引号( “ ” ),破折号( )和破折号( )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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