-2

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>
        <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}"     }
Community
  • 1
  • 1
AabinGunz
  • 12,109
  • 54
  • 146
  • 218

2 Answers2

3

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

See RegEx match open tags except XHTML self-contained tags

Community
  • 1
  • 1
Clement Herreman
  • 10,274
  • 4
  • 35
  • 57
  • Of course you're right in principle. But in this simple case (machine-generated, well-formed XML without comments or quoted strings) I think you can get away with regexes if you're aware that it's a quick and dirty solution. – Tim Pietzcker Sep 02 '11 at 08:19
  • Machine generated usually meaning that a developper created the program that generate it, I'm as worried about it's validity as if it was manually written! More seriously, using the right tool to the right task is important. Regex just aren't suited to parse XML ontheir own. – Clement Herreman Sep 02 '11 at 08:25
1

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());
} 
Tim Pietzcker
  • 328,213
  • 58
  • 503
  • 561