0

我有一个字符串

"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"工作

4

4 回答 4

2

你可以试试下一个:

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)
于 2013-08-26T15:58:04.070 回答
1

我建议使用正则表达式匹配来检查这一点。

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
}
于 2013-08-26T09:12:45.833 回答
1

你是说这个算法吗?

计算双引号的数量。如果有偶数,什么也不做。如果有奇数,将最后一个双引号替换为\"

于 2013-08-26T08:41:38.553 回答
0

在不使用循环的情况下,以下代码应该可以工作:

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
于 2013-08-26T09:10:52.550 回答