使用Java中的正则表达式在双引号之间提取子字符串 [英] Extract a substring between double quotes with regular expression in Java

查看:483
本文介绍了使用Java中的正则表达式在双引号之间提取子字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的字符串:

"   @Test(groups = {G1}, description = "adc, def")"

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

I want to extract "adc, def" (without quotes) using regexp in Java, how should I do?

推荐答案

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

If you really want to use regex:

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

说明:

.*   - anything
\\\" - quote (escaped)
(.*) - anything (captured)
\\\" - another quote
.*   - anything

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

However, it's a lot easier to not use regex:

"your \"string\" here".split("\"")[1]

这篇关于使用Java中的正则表达式在双引号之间提取子字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆