提问者:小点点

如何在StringBuffer Java中捕获某些文本[重复]


我有一个包含以下内容的StringBuffer对象:

<ET>read input: 1.629ms</ET>
<ET>There were 3 errors:
<Error>
    <ErrorId>AllConditionsTrue</ErrorId>
    <MetaData>
        <Entry>
            <Key>Balance Due</Key>
            <Value>1500.99</Value>
        </Entry>
    </MetaData>
</Error>

<Error>
    <ErrorId>Opposite</ErrorId>
    <MetaData>
        <Entry>
            <Key>Node</Key>
        </Entry>
    </MetaData>
</Error>

<Error>
    <ErrorId>minInclusive</ErrorId>
    <MetaData>
        <Entry>
            <Key>Description</Key>
            <Value>Wages Amount</Value>
        </Entry>
    </MetaData>
</Error>

: 0.027ms</ET>
<ET>convert: 319.414ms</ET>
<FORM id="123"/>
<DATA size="11920"/>
<ERROR code="0"/>
<Error>
    <ErrorId>AllConditionsTrue</ErrorId>
    <MetaData>
        <Entry>
            <Key>Balance Due</Key>
            <Value>1500.99</Value>
        </Entry>
    </MetaData>
</Error>

<Error>
    <ErrorId>Opposite</ErrorId>
    <MetaData>
        <Entry>
            <Key>Node</Key>
        </Entry>
    </MetaData>
</Error>

<Error>
    <ErrorId>minInclusive</ErrorId>
    <MetaData>
        <Entry>
            <Key>Description</Key>
            <Value>Wages Amount</Value>
        </Entry>
    </MetaData>
</Error>
Pattern p = Pattern.compile("<Error>.*?<\\/Error>", Pattern.DOTALL);
Matcher m = p.matcher(buf.toString());

String errorText = "";

while (m.find()) {
    errorText = m.group(1);
}
<Error>
    <ErrorId>minInclusive</ErrorId>
    <MetaData>
        <Entry>
            <Key>Description</Key>
            <Value>Wages Amount</Value>
        </Entry>
    </MetaData>
</Error>

共1个答案

匿名用户

Regex:

<Error>.*?<\/Error>

演示