62,612
社区成员
发帖
与我相关
我的任务
分享
String orginal= "\"haha\"123\"hehe\"4567\"hihi\"";
String replaced = orginal.replaceAll("\"(.*?)\"", "“$1”");
String orginal= "\"\"haha\"123\"hehe\"4567\"hihi\"\"";
String replaced = orginal.replaceAll("\"(.*)\"", "“$1”");
String s = replaced.replaceAll("\"(.*?)\"", "“$1”");
System.out.println(replaced);
System.out.println(s);
//package com.ricky.www;
/*
如这样一串字符串 【实现要转移的字符把"单引号"变成"双引号"】
用java如何才能把其中的英文双引号变成中文的呢?要考虑前后引号,前面的是“,后面的是”。
返回结果:【实现要转移的字符把“单引号”变成“双引号”】
*/
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Test{
public static void main(String[] args){
String content = "【实现要转移的字符把\"单引号\"变成\"双引号\"】";
String result = process(content);
}
public static String process(String content){
String regex = "(.*)\"(.*)\"(.*)";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(content);
while(matcher.find()){
content = matcher.group(1) + "“" + matcher.group(2) + "”" + matcher.group(3);
matcher = pattern.matcher(content);
}
System.out.println(content);
return content;
}
}
//Sample
import java.util.regex.*;
public class RegexSample {
/**
* @param args
*/
public static void main(String[] args) {
String regex = "\"\\w*?\"";//正则表达式匹配串
String str,str2;
String strOri = "haha\"123\"hehe\"4567\"hihi";//待处理字符串
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(strOri);
while(matcher.find())
{
str = matcher.group();
str2 = str;
str2 = str2.replaceAll("\"", "");//去掉前后引号
str2 = "“"+str2+"”"; //前后添上中文引号
strOri = strOri.replace(str, str2);
}
System.out.println(strOri);//输出结果
}
}