0

すべてのコンマを、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[] 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));
              }
          }
      }
}
}

上記のコードを使用すると、引用符内に存在する文字列の最初の出現のみを見つけることができ、2番目の文字列(「赤、青」)ではありません。

4

3 に答える 3

2

次のコードが機能するはずです。

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:アンバランスな二重引用符がなく、二重引用符がエスケープされていないと仮定します。

于 2013-08-15T18:11:57.010 に答える
0

また、以下も機能するはずです(つまり、テストしていません)。

//Catches contents of quotes
static final Pattern outer = Pattern.compile("\\\"[^\\\"]*\\\"");

private String replaceCommasInsideQuotes(String s) {
    StringBuilder bdr = new StringBuilder(s);
    Matcher m = outer.matcher(bdr);
    while (m.find()) {
        bdr.replace(m.start(), m.end(), m.group().replaceAll(",", "#"));
    }
    return bdr.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();
}
于 2013-08-15T19:37:56.357 に答える
0

これを試して

  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());
于 2013-08-15T18:31:03.873 に答える