使用Java中的Regex在双引号之间的字符串

时间:2014-07-30 18:31:13

标签: java

如何在Java中使用Regex在双引号之间获取字符串?

_settext(_textbox(0,_near(_span("My Name"))) ,"Brittas John");

前:我需要我的名字和布里塔斯约翰

2 个答案:

答案 0 :(得分:3)

从括号(...)

中包含的索引1中获取匹配的组
"([^"]*)"

DEMO

模式说明:

  "                        '"'
  (                        group and capture to \1:
    [^"]*                    any character except: '"' (0 or more times) (Greedy)
  )                        end of \1
  "                        '"'

示例代码:

Pattern p = Pattern.compile("\"([^\"]*)\"");
Matcher m = p.matcher("_settext(_textbox(0,_near(_span(\"My Name\"))) ,\"Brittas John\");");
while (m.find()) {
    System.out.println(m.group(1));
}

答案 1 :(得分:2)

试试这个正则表达式..

public static void main(String[] args) {
    String s = "_settext(_textbox(0,_near(_span(\"My Name\"))) ,\"Brittas John\");";
    Pattern p = Pattern.compile("\"(.*?)\"");
    Matcher m = p.matcher(s);
    while (m.find()) {
        System.out.println(m.group(1));
    }
}

O / P:

My Name
Brittas John