简体   繁体   English

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

[英]String between double quotes using Regex in Java

How can i get Strings between double quotes using Regex in Java? 如何在Java中使用Regex获得双引号之间的字符串?

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

ex: I need My Name and Brittas John 例如:我需要我的名字和布里塔斯·约翰

Get the matched group from index 1 that is captured by enclosing inside the parenthesis (...) 通过将其括在圆括号(...) ,从索引1中获取匹配的组(...)

"([^"]*)"

DEMO 演示

Pattern explanation: 模式说明:

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

sample code: 样例代码:

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));
}

Try this regex.. 试试这个正则表达式。

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 : O / P:

My Name
Brittas John

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM