从正则表达式获取无与伦比的字符串(Android)

时间:2016-01-07 17:55:20

标签: android regex

Pattern p = Pattern.compile(".*\\\"(.*)\\\".*");
Matcher m = p.matcher("\"Hi there\"! How are you");
if (m.find()) {
    try {
        String iGotMyMatchedString = m.group(0);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

上面的代码会在双引号内返回字符串,即“Hi there”。 我怎样才能获得“你好”的字母“<!p>”

1 个答案:

答案 0 :(得分:3)

您无法获得不匹配的字符串,而是可以在第二部分周围使用捕获组,并通过调用匹配对象上的".*\\\"(.*)\\\"(.*)" 来访问它:

gcloud docker search gcr.io/your-registry