我在Regex很糟糕,非常感谢这个问题的任何帮助,我认为对于任何熟悉的人来说都是新手。
我从REST调用中得到这样的响应
{"responseData":{"translatedText":"Ciao mondo"},"responseDetails":"","responseStatus":200,"matches":[{"id":"424913311","segment":"Hello World","translation":"Ciao mondo","quality":"74","reference":"","usage-count":50,"subject":"All","created-by":"","last-updated-by":null,"create-date":"2011-12-29 19:14:22","last-update-date":"2011-12-29 19:14:22","match":1},{"id":"0","segment":"Hello World","translation":"Ciao a tutti","quality":"70","reference":"Machine Translation provided by Google, Microsoft, Worldlingo or the MyMemory customized engine.","usage-count":1,"subject":"All","created-by":"MT!","last-updated-by":null,"create-date":"2012-05-14","last-update-date":"2012-05-14","match":0.85}]}
我所需要的就是那些引文之间的'Ciao mondo'。我希望使用Java的Split功能,我可以做到这一点,但不幸的是它不允许两个单独的分隔符,因为我可以在翻译之前指定文本。
为了简化,我坚持的是正则表达式,以收集在translateText“:”和下一个“
之间的任何内容我非常感谢任何帮助
答案 0 :(得分:3)
您可以使用\"translatedText\":\"([^\"]*)\"
表达式来捕获匹配项。
表达式含义如下:找到引用translatedText
后跟冒号和开头引号。然后匹配以下引号之前的每个字符,并将结果捕获到捕获组。
String s = " {\"responseData\":{\"translatedText\":\"Ciao mondo\"},\"responseDetails\":\"\",\"responseStatus\":200,\"matches\":[{\"id\":\"424913311\",\"segment\":\"Hello World\",\"translation\":\"Ciao mondo\",\"quality\":\"74\",\"reference\":\"\",\"usage-count\":50,\"subject\":\"All\",\"created-by\":\"\",\"last-updated-by\":null,\"create-date\":\"2011-12-29 19:14:22\",\"last-update-date\":\"2011-12-29 19:14:22\",\"match\":1},{\"id\":\"0\",\"segment\":\"Hello World\",\"translation\":\"Ciao a tutti\",\"quality\":\"70\",\"reference\":\"Machine Translation provided by Google, Microsoft, Worldlingo or the MyMemory customized engine.\",\"usage-count\":1,\"subject\":\"All\",\"created-by\":\"MT!\",\"last-updated-by\":null,\"create-date\":\"2012-05-14\",\"last-update-date\":\"2012-05-14\",\"match\":0.85}]}";
System.out.println(s);
Pattern p = Pattern.compile("\"translatedText\":\"([^\"]*)\"");
Matcher m = p.matcher(s);
if (!m.find()) return;
System.out.println(m.group(1));
答案 1 :(得分:0)
使用预测和后视来收集引号内的字符串: (?&LT = [{}:] \ “)。?*(?= \”)
class Test
{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
String in = scanner.nextLine();
Matcher matcher = Pattern.compile("(?<=[,.{}:]\\\").*?(?=\\\")").matcher(in);
while(matcher.find())
System.out.println(matcher.group());
}
}
答案 2 :(得分:0)
试试这个正则表达式 -
^.*translatedText":"([^"]*)"},"responseDetails".*$
匹配组将包含Ciao mondo文本。
这假设translatedText和responseDetails将始终出现在样本中指定的位置。