在Java中使用正则表达式在双引号之间提取子字符串

use*_*169 8 java regex

我有一个像这样的字符串:

"   @Test(groups = {G1}, description = "adc, def")"
Run Code Online (Sandbox Code Playgroud)

我想在Java中使用regexp提取"adc,def"(不带引号),我该怎么办?

Doo*_*nob 16

如果你真的想使用正则表达式:

Pattern p = Pattern.compile(".*\\\"(.*)\\\".*");
Matcher m = p.matcher("your \"string\" here");
System.out.println(m.group(1));
Run Code Online (Sandbox Code Playgroud)

说明:

.*   - anything
\\\" - quote (escaped)
(.*) - anything (captured)
\\\" - another quote
.*   - anything
Run Code Online (Sandbox Code Playgroud)

但是,不使用正则表达式要容易得多:

"your \"string\" here".split("\"")[1]
Run Code Online (Sandbox Code Playgroud)

  • @cody`string.split("\"")[1]` (2认同)