Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/343.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java从字符串中提取_Java_Regex_Arrays_Methods_Substring - Fatal编程技术网

java从字符串中提取

java从字符串中提取,java,regex,arrays,methods,substring,Java,Regex,Arrays,Methods,Substring,我有一个字符串数组,类似于 <div id="option1">hello</div> <div style="color: cyan">world</div> 你好 世界 有没有办法从div中提取信息?我已经写了一些东西,但它不是动态的(我必须指定位的长度),这在我的应用程序中是无用的,因为数组中的内容不总是相同的 希望您能理解我的问题,如果您需要更多信息,我会尽快回复 我正在使用java。正如@SLaks所说,使用HTML解析器。Jav

我有一个字符串数组,类似于

 <div id="option1">hello</div>
 <div style="color: cyan">world</div>
你好 世界 有没有办法从div中提取信息?我已经写了一些东西,但它不是动态的(我必须指定位的长度),这在我的应用程序中是无用的,因为数组中的内容不总是相同的

希望您能理解我的问题,如果您需要更多信息,我会尽快回复


我正在使用java。

正如@SLaks所说,使用HTML解析器。Java有很多好的工具。我最喜欢的是。

正如@SLaks所说,使用HTML解析器。Java有很多好的工具。我最喜欢的是。

一个完整的例子:

请注意,示例中的HTML是格式良好的XML,也可以使用任何XML解析器进行解析。在处理格式不正确的输入时,需要一个特定于HTML的解析器。

完整示例:


请注意,示例中的HTML是格式良好的XML,也可以使用任何XML解析器进行解析。在处理格式不正确的输入时,您需要一个特定于HTML的解析器。

如果您知道只有一组HTML标记,如果您知道它是什么标记就更好了,您可以执行以下操作:

String[] html = new String[] { 
    "<div id=\"option1\">hello</div>",
    "<div style=\"color: cyan\">world</div>" };

for(String index : html){
    int firstEnd = index.firstIndexOf("/>");
    int lastBeginning = index.indexOf("<", 2); // Could become "</div>

    String contents = index.substring(firstEnd + 1, lastBeginning - 1);
    System.out.println(contents);
}
String[]html=新字符串[]{
“你好”,
“世界”};
for(字符串索引:html){
int firstEnd=index.firstIndexOf(“/>”);

int lastBeging=index.indexOf(“如果您知道只有一组HTML标记,如果您知道它是什么标记就更好了,您可以执行以下操作:

String[] html = new String[] { 
    "<div id=\"option1\">hello</div>",
    "<div style=\"color: cyan\">world</div>" };

for(String index : html){
    int firstEnd = index.firstIndexOf("/>");
    int lastBeginning = index.indexOf("<", 2); // Could become "</div>

    String contents = index.substring(firstEnd + 1, lastBeginning - 1);
    System.out.println(contents);
}
String[]html=新字符串[]{
“你好”,
“世界”};
for(字符串索引:html){
int firstEnd=index.firstIndexOf(“/>”);

int lastBeging=index.indexOf(“您需要一个HTML解析器。Chuck Norris在这里使用正则表达式:)你试过java?Sax?Xerces中可用的一些XML解析器吗?@PetarMinchev,不,Chuck Norris不使用正则表达式。数据看到他来了,自己解析。尽管有时候正则表达式可以很好地与HTML一起工作,但你不敢使用它。你需要一个HTML解析器。Chuck Norris在这里使用正则表达式:)你试过java?Sax?Xerces中可用的一些XML解析器吗?@PetarMinchev,不,Chuck Norris不使用regex。数据看到他来自己解析。尽管有时它可以很好地与HTML一起工作,但你不敢使用regex。谢谢,除了使用外部库之外,还有其他方法吗?不太有。java有一个很好的XML解析器“内置”,但HTML是一个非常不同的野兽。谢谢,除了使用外部库之外,还有其他方法可以这样做吗?没有。Java有一个很好的XML解析器“内置”,但HTML是一个非常不同的野兽。
String[] html = new String[] { 
    "<div id=\"option1\">hello</div>",
    "<div style=\"color: cyan\">world</div>" };

for(String index : html){
    int firstEnd = index.firstIndexOf("/>");
    int lastBeginning = index.indexOf("<", 2); // Could become "</div>

    String contents = index.substring(firstEnd + 1, lastBeginning - 1);
    System.out.println(contents);
}
String[] html = new String[] { 
                "<div id=\"option1\">hello</div>",
                "<div style=\"color: cyan\">world</div>" };

        String tag = "div";
        Pattern p = Pattern.compile("<" + tag + ".*?>(.*?)</" + tag + ">");
        Matcher m;

        for(String index : html){
            m = p.matcher(index);
            while(m.find()) System.out.println(m.group(1));
        }