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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1473155/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 12:39:16  来源:igfitidea点击:

how to get data between quotes in java?

javaquotestokenize

提问by atomsfat

I have this lines of text the number of quotes could change like:

我有这行文本,引号的数量可能会改变,例如:

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"

How I obtain the data between the quotes?

我如何获得引号之间的数据?

The result should be:

结果应该是:

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

comillas
mas, comillas, 把戏
a, word, are, comillas

采纳答案by erickson

You can use a regular expression to fish out this sort of information.

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

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

This example assumes that the language of the line being parsed doesn't support escape sequences for double-quotes within string literals, contain strings that span multiple "lines", or support other delimiters for strings like a single-quote.

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

回答by jsight

String line = "if(getip(document.referrer)==\"www.eg.com\" || getip(document.referrer)==\"192.57.42.11\"";
StringTokenizer stk = new StringTokenizer(line, "\"");
stk.nextToken();
String egStr = stk.nextToken();
stk.nextToken();
String ipStr = stk.nextToken();

回答by Stephane Grenier

Firstly, please note that you should user equals() rather than ==. "==" by default asks if they are the same instance in memory, which in Strings can sometimes be the case. With myString.equals("...") you're comparing the values of the Strings.

首先,请注意您应该使用 equals() 而不是 ==。默认情况下,“==”会询问它们是否是内存中的同一个实例,在字符串中有时可能会出现这种情况。使用 myString.equals("...") 比较字符串的值。

As for how do you obtain the values between the quotes I'm not sure what you mean. "..." is an actual object. Alternatively you could do:

至于你如何获得引号之间的值,我不确定你的意思。“...”是一个实际的对象。或者你可以这样做:

String webUrl = "www.eg.com";

String webUrl = "www.eg.com";

回答by Uri

If you are parsing an entire source file rather than just one line, a parser based on the grammar of the function might be a safer choice than trying to do this based on strings.

如果您要解析整个源文件而不仅仅是一行,那么基于函数语法的解析器可能比尝试基于字符串来解析更安全。

I am guessing that these would be string literals in your grammar.

我猜这些将是您语法中的字符串文字。

回答by SingleShot

Check out StringUtilsin the Apache commons-lang library - it has a substringsBetweenmethod.

看看StringUtils在Apache的commons-Lang库-它有一个substringsBetween方法。

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

回答by Dani

If you want to get all ocurrences from a file:

如果你想从一个文件中获取所有的 occurrences

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class testReadQuotes {


    public static void main(String args[]) throws IOException{

        Pattern patt = Pattern.compile("\"([^\"]*)\"");
        BufferedReader r = new BufferedReader(new FileReader("src\files\myFile.txt"));

        String line;

        while ((line = r.readLine()) != null) {

          Matcher m = patt.matcher(line);

          while (m.find()) {
            System.out.println(m.group(0));
          }

        }

    }

}