使用java正则表达式将逗号替换为字符串中的任何特殊字符
我想用java中双引号中的任何一个特殊字符(例如“#”)替换所有逗号 以下是字符串:使用java正则表达式将逗号替换为字符串中的任何特殊字符,java,regex,Java,Regex,我想用java中双引号中的任何一个特殊字符(例如“#”)替换所有逗号 以下是字符串: String line="\"Lee, Rounded, Neck, Printed\",410.00,300.00,\"Red , Blue\",lee"; 输出: "Lee# Rounded# Neck# Printed",410.00,300.00,"Red # Blue",lee 我试过这个: public class Str { public static void main(String
String line="\"Lee, Rounded, Neck, Printed\",410.00,300.00,\"Red , Blue\",lee";
输出:
"Lee# Rounded# Neck# Printed",410.00,300.00,"Red # Blue",lee
我试过这个:
public class Str {
public static void main(String[] args) {
String line="\"Lee, Rounded, Neck, Printed\",410.00,300.00,\"Red , Blue\",lee";
String lineDelimiter=",";
String templine=line;
if(templine!=null && templine.contains("\""))
{
Pattern p=Pattern.compile("(\".*?"+Pattern.quote(lineDelimiter)+".*?\")");
Matcher m=p.matcher(templine);
if(m.find())
{
for (int i = 1; i <= m.groupCount(); i++) {
String Temp=m.group(i);
String Temp1=Temp;
Temp=Temp.replaceAll("(,)", " ## ");
line=line.replaceAll(Pattern.quote(Temp1),Pattern.quote(Temp));
}
}
}
}
}
公共类Str{
公共静态void main(字符串[]args){
字符串行=“\”Lee,圆形,颈部,印刷\”,410.00300.00,\“红色,蓝色\”,Lee;
字符串lineDelimiter=“,”;
字符串模板=行;
if(templine!=null&&templine.contains(“\”)
{
Pattern p=Pattern.compile(“(\”*?“+Pattern.quote(lineDelimiter)+“*?\”);
Matcher m=p.Matcher(模板线);
if(m.find())
{
对于(int i=1;i以下代码应适用:
String line="\"Lee, Rounded, Neck, Printed\",410.00,300.00,\"Red , Blue\",lee";
String repl = line.replaceAll(",(?!(([^\"]*\"){2})*[^\"]*$)", "#");
System.out.println("Replaced => " + repl);
输出:
"Lee# Rounded# Neck# Printed",410.00,300.00,"Red # Blue",lee
解释:这个正则表达式的基本意思是,如果逗号后面没有偶数个双引号,则匹配逗号。换句话说,如果逗号在双引号内,则匹配逗号
PS:假设没有不平衡的双引号,也没有转义的双引号。试试这个
String line="\"Lee, Rounded, Neck, Printed\",410.00,300.00,\"Red , Blue\",lee";
StringTokenizer st=new StringTokenizer(line,"\"");
List<String> list=new ArrayList<>();
List<String> list2=new ArrayList<>();
while (st.hasMoreTokens()){
list.add(st.nextToken());
}
Pattern p = Pattern.compile("\"([^\"]*)\"");
Matcher m = p.matcher(line);
StringBuilder sb=new StringBuilder();
while (m.find()) {
list2.add(m.group(1));
}
for(String i:list){
if(list2.contains(i)){
sb.append(i.replaceAll(",","#"));
}else{
sb.append(i);
}
}
System.out.println(sb.toString());
String line=“\”Lee,圆形,颈部,打印\”,410.00300.00,\“红色,蓝色\”,Lee”;
StringTokenizer st=新的StringTokenizer(行“\”);
列表=新的ArrayList();
List list2=新的ArrayList();
而(st.hasMoreTokens()){
添加(st.nextToken());
}
Pattern p=Pattern.compile(“\”([^\“]*)\”);
匹配器m=p.匹配器(线);
StringBuilder sb=新的StringBuilder();
while(m.find()){
列表2.添加(m.组(1));
}
for(字符串i:列表){
如果(列表2.包含(i)){
sb.追加(i.replaceAll(“,”,“#”);
}否则{
某人(i);
}
}
System.out.println(sb.toString());
以下功能也应该可以使用(这意味着我还没有测试过它):
或类似的:
//Catches contents of quotes
static final Pattern outer = Pattern.compile("\\\"[^\\\"]*\\\"");
private String replaceCommasInsideQuotes(String s) {
StringBuffer buff = new StringBuffer();
Matcher m = outer.matcher(bdr);
while (m.find()) {
m.appendReplacement(buff, "");
buff.append(m.group().replaceAll(",", "#"));
}
m.appendTail(buff);
return buff.toString();
}
你不需要正则表达式来进行简单的字符替换,使用正则表达式也不难,你遇到了什么问题?关键是“内部双引号”。然而,你尝试了什么?你考虑过使用CSV库吗?@GiulioFranco我没有考虑使用CSV库。
//Catches contents of quotes
static final Pattern outer = Pattern.compile("\\\"[^\\\"]*\\\"");
private String replaceCommasInsideQuotes(String s) {
StringBuffer buff = new StringBuffer();
Matcher m = outer.matcher(bdr);
while (m.find()) {
m.appendReplacement(buff, "");
buff.append(m.group().replaceAll(",", "#"));
}
m.appendTail(buff);
return buff.toString();
}