簡體   English   中英

Java替換不成對的雙引號

[英]Java Replace double quote which is not in pair

我有一個像

"ABC def" xxy"u

我想替換成對的雙引號。

因此,在上面的示例中,我只想替換xxy"u雙引號而不是成對的前兩個。

輸出應采用這種格式。

"ABC def" xxy\"u

應該與每個不成對的雙引號一起使用- "111" "222" "333" "4因此這里"之前的4應該替換為\\"

提前致謝。

如果它也將檢測實際的貨幣對,而不是最后一個雙引號,那將是很好的。 EX: "AAA" "bbb" "CCC "DDD" ->應替換為"AAA" "bbb" \\"CCC "DDD"

這就是我正在使用的

    int totalCountOfDQ = countOccurence(s, '"');
    int lastIndexOfDQ = s.lastIndexOf('"');
    if(totalCountOfDQ % 2 == 1){
        String start = s.substring(0, lastIndexOfDQ);
        String end = s.substring(lastIndexOfDQ+1);
        s = start + "\\\"" + end;
    }

並且它在我的示例中正常工作以為它無法正常工作"4 "111" "222"

您可以嘗試下一個:

private static final Pattern REGEX_PATTERN =
        Pattern.compile("\\B\"\\w*( \\w*)*\"\\B");

private static String replaceNotPairs(String input) {
    StringBuffer sb = new StringBuffer();
    Matcher matcher = REGEX_PATTERN.matcher(input);
    int start = 0;
    int last = 0;
    while (matcher.find()) {
        start = matcher.start();
        sb.append(input.substring(last, start).replace("\"", "\\\""));
        last = matcher.end();
        sb.append(matcher.group());
    }
    sb.append(input.substring(last).replace("\"", "\\\""));
    return sb.toString();
}

例如:

public static void main(String[] args) {
    System.out.printf("src: %s%nout: %s%n%n",
            "\"ABC def\" xxy\"u",
            replaceNotPairs("\"ABC def\" xxy\"u"));
    System.out.printf("src: %s%nout: %s%n%n",
            "\"111\" \"222\" \"333\" \"4",
            replaceNotPairs("\"111\" \"222\" \"333\" \"4"));
    System.out.printf("src: %s%nout: %s%n%n",
            "\"AAA\" \"bbb\" \"CCC \"DDD\"",
            replaceNotPairs("\"AAA\" \"bbb\" \"CCC \"DDD\""));
    System.out.printf("src: %s%nout: %s%n%n",
            "\"4 \"111\" \"222\"",
            replaceNotPairs("\"4 \"111\" \"222\""));
    System.out.printf("src: %s%nout: %s%n%n",
            "\"11\" \"2 \"333\"",
            replaceNotPairs("\"11\" \"2 \"333\""));
}

示例輸入的輸出:

src: "ABC def" xxy"u
out: "ABC def" xxy\"u

src: "111" "222" "333" "4
out: "111" "222" "333" \"4

src: "AAA" "bbb" "CCC "DDD"
out: "AAA" "bbb" \"CCC "DDD"

src: "4 "111" "222"
out: \"4 "111" "222"

src: "11" "2 "333"
out: "11" \"2 "333"

請參閱正則表達式的說明:

\B\"\w*( \w*)*\"\B

正則表達式可視化

(來自http://rick.measham.id.au/paste/explain.pl?regex ):

NODE                     EXPLANATION
----------------------------------------------------------------------------
  \B                       the boundary between two word chars (\w)
                           or two non-word chars (\W)
----------------------------------------------------------------------------
  \"                       '"'
----------------------------------------------------------------------------
  \w*                      word characters (a-z, A-Z, 0-9, _) (0 or
                           more times (matching the most amount
                           possible))
----------------------------------------------------------------------------
  (                        group and capture to \1 (0 or more times
                           (matching the most amount possible)):
----------------------------------------------------------------------------
                             ' '
----------------------------------------------------------------------------
    \w*                      word characters (a-z, A-Z, 0-9, _) (0 or
                             more times (matching the most amount
                             possible))
----------------------------------------------------------------------------
  )*                       end of \1 (NOTE: because you are using a
                           quantifier on this capture, only the LAST
                           repetition of the captured pattern will be
                           stored in \1)
----------------------------------------------------------------------------
  \"                       '"'
----------------------------------------------------------------------------
  \B                       the boundary between two word chars (\w)
                           or two non-word chars (\W)

你是說這個算法嗎?

計算雙引號的數量。 如果有偶數,則什么也不做。 如果有奇數,請用\\"替換最后一個雙引號\\"

我建議使用正則表達式匹配來檢查這一點。

Pattern myPattern = Pattern.compile("\".*\"");
Pattern myPattern1 = Pattern.compile("\"([^\"]*)$");
var input=yourString;//assign your string to a new variable
input=input.replaceAll(myPattern,' match ');//replace all portions in " with your own string
if(input.matches("\"")) {
   yourString.replaceAll(myPattern1,/\\/);//if there is a dangling ", replace with a \ in your original string
}

在不使用循環的情況下,以下代碼應該起作用:

String s = "\"111 \" \" 222\" \" 333\" \"4";
// s.replaceAll("[^\"]+", "").length() gives count of " in String
if (s.replaceAll("[^\"]+", "").length() % 2 == 1) {
    int i = s.lastIndexOf('"');
    s = s.substring(0, i) + "\\\"" + s.substring(i+1);
}
System.out.println(s); // "111 " " 222" " 333" \"4

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM