如何在java中获取引号之间的数据?

2022-09-01 10:11:46

我有这行文本,引号的数量可以像这样变化:

Here just one "comillas"
But I also could have more "mas" values in "comillas" and that "is" the "trick"
I was thinking in a method that return "a" list of "words" that "are" between "comillas"

我如何获得报价之间的数据?

结果应该是:

comillas
mas, comillas, trick
a, words, are, comillas


答案 1

您可以使用正则表达式来筛选出此类信息。

Pattern p = Pattern.compile("\"([^\"]*)\"");
Matcher m = p.matcher(line);
while (m.find()) {
  System.out.println(m.group(1));
}

此示例假定所分析的行的语言不支持字符串文本中双引号的转义序列,包含跨多个“行”的字符串,或支持字符串(如单引号)的其他分隔符。


答案 2

在Apache commons-lang库中查看 - 它有一个方法。StringUtilssubstringsBetween

String lineOfText = "if(getip(document.referrer)==\"www.eg.com\" || getip(document.referrer)==\"192.57.42.11\"";

String[] valuesInQuotes = StringUtils.substringsBetween(lineOfText , "\"", "\"");

assertThat(valuesInQuotes[0], is("www.eg.com"));
assertThat(valuesInQuotes[1], is("192.57.42.11"));

推荐