如何在Java中使用正则表达式获取子字符串?

时间:2015-09-21 13:54:58

标签: java regex

这是我的字符串

 "fulladdress" : "210 Main Ln, Chicago, DE 72123, USA",
            "destination" : "whatever . 
            .... 

这是我的代码。我想在"fulladdress" :",

之间获得子串
 String myString =   "],\n" + 
        "\"fulladdress\" : \"210 Main Ln, Chicago, DE 72123, USA\",\n" + 
        "\"destination\" : \"whatever . " + 
        ".... " ; 
        Pattern pattern = Pattern.compile("^(\"fulladdress\" :).*$\",\n"); 
        Matcher matcher = pattern.matcher(myString); 
        if (matcher.find()) {
            System.out.println(matcher.group(1));  
        }

但这不会给我210 Main Ln, Chicago, DE 72123, USA

我该如何改进?

1 个答案:

答案 0 :(得分:1)

您不应使用^(星号)和$(结束)锚点,因为您要匹配的模式位于字符串之间。

使用以下内容:

Pattern pattern = Pattern.compile("(\"fulladdress\" :).*\",\n");