简体   繁体   English

如何在两个字符串之间找到值?

[英]How do I find a value between two strings?

How would I "find" and "get" a value between two strings? 我如何“找到”和“获取”两个字符串之间的值?

ie: <a>3</a> 即: <a>3</a>

I'm reading a file to find the location of <a> , where that starts, then it will stop reading when it finds </a> The value I want to return is "3". 我正在读取一个文件来找到<a>的位置,在那里开始,然后它会在找到时停止读取</a>我要返回的值是“3”。

Using JRE 6 使用JRE 6

Your two main options are: 您的两个主要选项是:

1) preferred but potentially complicated : using an XML/HTML parser and getting the text within the first "a" element. 1) 首选但可能很复杂 :使用XML / HTML解析器并在第一个“a”元素中获取文本。 eg using Jsoup (thanks @alpha123): 例如使用Jsoup (感谢@ alpha123):

Jsoup.parse("<a>3</a>").select("a").first().text(); // => "3"

2) easier but not very reliable : using a regular expression to extract the characters between the <a> and </a> strings. 2) 更容易但不太可靠 :使用正则表达式提取<a></a>字符串之间的字符。 eg: 例如:

String s = "<a>3</a>";
Pattern p = Pattern.compile("<a>(.*?)</a>")
Matcher m = p.matcher(s);
if (m.find()) {
  System.out.println(m.group(1)); // => "3"
}

Jsoup将很容易做到这一点。

String title = Jsoup.parse("<a>3</a>").select("a").first().text();

You can use regex: 你可以使用正则表达式:

try {
    Pattern regex = Pattern.compile("<a>(.*)</a>");
    Matcher regexMatcher = regex.matcher(subjectString);
    while (regexMatcher.find()) {
        for (int i = 1; i <= regexMatcher.groupCount(); i++) {
            // matched text: regexMatcher.group(i)
            // match start: regexMatcher.start(i)
            // match end: regexMatcher.end(i)
        }
    } 
} catch (PatternSyntaxException ex) {
    // Syntax error in the regular expression
}

But, if your input is HTML, you should really consider using an HTML parser. 但是,如果您的输入是HTML,您应该考虑使用HTML解析器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM