使用StringUtils的substringBetween()方法获取两个标签之间的文本

时间:2018-10-23 07:39:32

标签: java apache-stringutils

我输入的内容如下:

NAME
Get-AzureRmSqlServer

SYNOPSIS
Returns information about SQL Database servers.


SYNTAX
Get-AzureRmSqlServer [[-ResourceGroupName] <System.String>] [[-ServerName] 
<System.String>] [-DefaultProfile <Microsoft.Azure.Commands.Common.Authenticatio
n.Abstractions.IAzureContextContainer>] [-Confirm 
<System.Management.Automation.SwitchParameter>] [-WhatIf 
<System.Management.Automation.SwitchParameter>] [<CommonParameters>]


DESCRIPTION
The Get-AzureRmSqlServer cmdlet returns information about one or more Azure SQL 
Database servers. Specify the name of a server to see information for only that 
server.

我想捕获地址标签之间的所有内容。

我尝试过:

<address>
    <addressLine>280 Flinders Mall</addressLine>
    <geoCodeGranularity>PROPERTY</geoCodeGranularity>
</address>
<address type="office">
    <addressLine>IT Park</addressLine>
    <geoCodeGranularity>office Space</geoCodeGranularity>
</address>

这不适用于所有情况,因为地址标记内部可能包含某些属性。请帮助如何获取此类字符串的文本。

3 个答案:

答案 0 :(得分:1)

通常,您不应该使用正则表达式来解析HTML / XML内容。而是使用XPath之类的解析器。鉴于您似乎无法使用解析器,我们可以使用模式匹配器尝试以下选项:

int count = 0;
String input = "<address>\n<addressLine>280 Flinders Mall</addressLine>\n    <geoCodeGranularity>PROPERTY</geoCodeGranularity>\n</address>\n<address type=\"office\">\n    <addressLine>IT Park</addressLine>\n    <geoCodeGranularity>office Space</geoCodeGranularity>\n</address>";
String pattern = "<address[^>]*>(.*?)</address>";
Pattern r = Pattern.compile(pattern, Pattern.DOTALL);
Matcher m = r.matcher(input);

while (m.find( )) {
    count += m.group(1).length();
    System.out.println("Found value: " + m.group(1) );
}

System.out.println("count = " + count);  

这会为您的示例数据中的两个<address>标签找到198个计数。

要使用BufferedReader进行此操作,您可能必须确保一次读取一个完整的<address>标签。

答案 1 :(得分:0)

您可以将文件转换为String,并可以如下确定所需子字符串的开始和结束索引:

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;

public class Address {

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

        // Complete File Path
        File dir =
            new File("\\..\\..\\Test.html");

        // Convert File Data As String
        String data =
            new String(
                Files.readAllBytes(Paths
                    .get(dir
                        .getAbsolutePath())));

        // For Loop to get all the <address> tags in the file.
        for (int index = data.indexOf("<address"); index >= 0;) {

            // Start Index
            int startIndex = data.indexOf(">", index + 1);
            ++startIndex;

            // End Index
            int indexOfEnd = data.indexOf("</address>", startIndex + 1);

            String attributesString = data.substring(startIndex, indexOfEnd);
            // Replace below line with desired logic with calling trim() on the String attributesString
            System.out.println(attributesString);

            // Next Address will be after the end of first address
            index = data.indexOf("<address", indexOfEnd + 1);
        }
    }
}

答案 2 :(得分:0)

while (scan.hasNextLine()) {

        parser = scan.nextLine();
        // System.out.println(parser);
        if (parser.equals("<adress>")) {
            parser = scan.nextLine();
            // System.out.println(parser);
            int startPosition = parser.indexOf("<adressLine>") + "<adressLine>".length();
            int endPosition = parser.indexOf("</adressLine>", startPosition);
            idNumber = parser.substring(startPosition, endPosition);
            parser = scan.nextLine();

            int startPosition1 = parser.indexOf("<geoCodeGranularity>") + "<geoCodeGranularity>".length();
            int endPosition1 = parser.indexOf("</geoCodeGranularity>", startPosition1);
            time = parser.substring(startPosition1, endPosition1);
            parser = scan.nextLine();

...... 算法必须是这样的。如果您在文件上阅读。