java regex用于不情愿的匹配

时间:2012-02-23 00:15:57

标签: java regex reluctant-quantifiers

需要找到以下问题的表达式:

String given = "{ \"questionID\" :\"4\", \"question\":\"What is your favourite hobby?\",\"answer\" :\"answer 4\"},{ \"questionID\" :\"5\", \"question\" :\"What was the name of the first company you worked at?\",\"answer\" :\"answer 5\"}";

我想得到什么:"{ \"questionID\" :\"4\", \"question\":\"What is your favourite hobby?\",\"answer\" :\"*******\"},{ \"questionID\" :\"5\", \"question\" :\"What was the name of the first company you worked at?\",\"answer\" :\"******\"}";

我在尝试什么:

    String regex = "(.*answer\"\\s:\"){1}(.*)(\"[\\s}]?)";
    String rep = "$1*****$3";
    System.out.println(test.replaceAll(regex, rep));

我得到的是什么:

"{ \"questionID\" :\"4\", \"question\":\"What is your favourite hobby?\",\"answer\" :\"answer 4\"},{ \"questionID\" :\"5\", \"question\" :\"What was the name of the first company you worked at?\",\"answer\" :\"******\"}";

由于贪婪行为,第一组捕获了两个“答案”部分,而我希望它在找到足够的部分后停止,执行替换,然后继续寻找。

2 个答案:

答案 0 :(得分:0)

以下正则表达式适用于我:

regex = "(?<=answer\"\\s:\")(answer.*?)(?=\"})";
rep = "*****";
replaceALL(regex,rep);

由于我在没有java的情况下进行测试,\"可能会被错误地转义。

http://regexr.com?303mm

答案 1 :(得分:0)

模式

("answer"\s*:\s*")(.*?)(")

似乎做你想做的事。这是Java的转义版本:

(\"answer\"\\s*:\\s*\")(.*?)(\")

此处的关键是使用(.*?)来匹配答案,而不是(.*)。后者匹配尽可能多的字符,前者将尽快停止。

如果答案中有双引号,则上述模式将不起作用。这是一个更复杂的版本,允许它们:

("answer"\s*:\s*")((.*?)[^\\])?(")

您必须在替换模式中使用$4而不是$3