Java中使用正则表达式的双引号之间的字符串
如何在Java中使用正则表达式获取双引号之间的字符串Java中使用正则表达式的双引号之间的字符串,java,Java,如何在Java中使用正则表达式获取双引号之间的字符串 _settext(_textbox(0,_near(_span("My Name"))) ,"Brittas John"); 例如:我需要我的名字和Brittas John从索引1中获取匹配的组,该索引通过在括号内括起来捕获(…) 模式说明: " '"' ( group and capture to \1: [^"]*
_settext(_textbox(0,_near(_span("My Name"))) ,"Brittas John");
例如:我需要我的名字和Brittas John从索引1中获取匹配的组,该索引通过在括号内括起来捕获
(…)
模式说明:
" '"'
( 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中获取匹配的组,该索引是通过在括号内括起来捕获的
(…)
模式说明:
" '"'
( 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));
}
试试这个正则表达式
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:
试试这个正则表达式
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:
但正如我所说,它的性能很慢。看看有没有步骤。我不确定它是否给出了真正的结果。@user3218114性能有多慢?在这两种情况下,解析器一看到
“
。查看提取所需结果所需的步骤数量。贪婪模式和懒惰模式的区别是什么?哪个更快?@user3218114 lazy实际上通常更快。它确切地知道什么时候停止。在这种特殊情况下,您的贪婪应该表现得同样出色。但我还是不明白为什么会更快。@user3218114-我明白了。。谢谢:)但是按照我的建议,它的性能很慢。看看有没有步骤。我不确定它是否给出了真正的结果。@user3218114性能有多慢?在这两种情况下,解析器一看到“
。查看提取所需结果所需的步骤数量。贪婪模式和懒惰模式的区别是什么?哪个更快?@user3218114 lazy实际上通常更快。它确切地知道什么时候停止。在这种特殊情况下,您的贪婪应该表现得同样出色。但我还是不明白为什么会更快。@user3218114-我明白了。。谢谢:)