开发者

Regex to fetch all matching values from xml node

开发者 https://www.devze.com 2023-04-01 06:43 出处:网络
Following this question it gives me just 1st match. I want to get all the matching into a string or a string array

Following this question it gives me just 1st match. I want to get all the matching into a string or a string array

This is my part of output from which I need to extract all Category's

<Trace Enabled="false">
        <ActiveCategories>
            <Category>ENVIRONMENT</Category>
            <Category>EXEC</Category>
            <Category>EXTERNALS</Category>
            <Category>FILESYSTEM</Category>
            <Category>INPUT_DOC</Category>
            <Category>INTERFACES</Category>
            <Category>NETWORKING</Category>
            <Category>OUTPUT_DOC</Category>
            <Category>PREPROCESSOR_INPUT</Category>
            <Category>REQUEST</Category>
            <Category>SYSTEMRESOURCES</Category>
            <Category>VIEWIO</Category>
            <Category>ALL</Category>
        </ActiveCategories>
        <SeverityLevel>ERROR</SeverityLevel>
        <MessageInfo>
            <ProcessAndThreadIds>true</ProcessAndThreadIds>
            <TimeStamp>true</TimeStamp>
        </MessageInfo>
      开发者_JAVA技巧  <TraceFile>
            <FileName>CMDS_log.txt</FileName>
            <MaxFileSize>1000000</MaxFileSize>
            <RecyclingMethod>Restart</RecyclingMethod>
        </TraceFile>
    </Trace>

right now through the below code I am only able to fetch ENVIRONMENT, I need to fetch all of the Category's value

def regexFinder(String myInput,String myRegex)
{
String ResultString
Pattern regex
Matcher regexMatcher

regex = Pattern.compile(myRegex, Pattern.DOTALL);
regexMatcher = regex.matcher(myInput);
if (regexMatcher.find()) {
    ResultString = regexMatcher.group();
}
}

tempResultString=regexFinder(ResultString,"(?<=<Category>)(?:(?!</Category>).)*")
    csm.cmengine_category(tempResultString)
    {           "${rs}"     }


Don't use regex to parse XML, use a parser.

See RegEx match open tags except XHTML self-contained tags


You need to apply .find() repeatedly to iterate over all results:

Matcher regexMatcher = regex.matcher(myInput);
List<String> matchList = new ArrayList<String>();
while (regexMatcher.find()) {
    matchList.add(regexMatcher.group());
} 
0

精彩评论

暂无评论...
验证码 换一张
取 消