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

发布于 2024-08-05 22:15:28 字数 340 浏览 3 评论 0原文

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

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
马斯、科米利亚斯、诡计
a、单词、are、comillas

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

那小子欠揍 2024-08-12 22:15:28

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

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

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

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.

半仙 2024-08-12 22:15:28

查看 Apache commons-lang 库中的 StringUtils - 它有一个 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"));

Check out StringUtils in the Apache commons-lang library - it has a substringsBetween method.

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"));
苏佲洛 2024-08-12 22:15:28
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();
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();
伪装你 2024-08-12 22:15:28

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

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

String webUrl = "www.eg.com";

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.

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

如何视而不见 2024-08-12 22:15:28

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

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

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.

滥情哥ㄟ 2024-08-12 22:15:28

如果您想获取文件中的所有事件

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

        }

    }

}

If you want to get all ocurrences from a file:

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

        }

    }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文