[英]regex - matching between a literal string and a quotation mark
我在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功能可以做到這一點,但是不幸的是,它不允許兩個單獨的定界符,因為這樣我可以在翻譯之前指定文本。
為簡化起見,我堅持使用的是正則表達式,以收集介於TranslationText“:”和下一個“
我將非常感謝您的幫助
您可以使用\\"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));
此片段打印Ciao mondo
。
使用前向和后向收集引號內的字符串:(?<= [,。{}:] \\“)。*?(?= \\”)
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());
}
}
試試這個正則表達式-
^.*translatedText":"([^"]*)"},"responseDetails".*$
匹配組將包含文本Ciao mondo。
假定TranslationText和responseDetails將始終出現在示例中指定的位置。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.