0

I have an output, where i'd like to fetch the value of CMEngine node i.e., everything inside CMEngine node. Please help me with a regex, I already have a java code in place which uses the regex, so I just need the regex. Thanks

My XML

<General>
    <LanguageID>en_US</LanguageID>
<CMEngine>
    <CMServer/> <!-- starting here -->
    <DaysToKeepHistory>4</DaysToKeepHistory>
    <PreprocessorMaxBuf>5000000</PreprocessorMaxBuf>
    <ServiceRefreshInterval>30</ServiceRefreshInterval>
    <ReuseMemoryBetweenRequests>true</ReuseMemoryBetweenRequests>
    <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>
        </ActiveCategories>
        <SeverityLevel>ERROR</SeverityLevel>
        <MessageInfo>
            <ProcessAndThreadIds>true</ProcessAndThreadIds>
            <TimeStamp>true</TimeStamp>
        </MessageInfo>
        <TraceFile>
            <FileName>CMEngine_log.txt</FileName>
            <MaxFileSize>1000000</MaxFileSize>
            <RecyclingMethod>Restart</RecyclingMethod>
        </TraceFile>
    </Trace>
    <JVMLocation>C:\Informatica\9.1.0\java\jre\bin\server</JVMLocation>
    <JVMInitParamList/>  <!-- Ending here -->
</CMEngine>
</General>
AabinGunz
  • 12,109
  • 54
  • 146
  • 218
  • not sure what you'd like to achieve but see if this can help: http://stackoverflow.com/questions/7008466/selecting-xml-raw-text – Mateusz Chromiński Aug 30 '11 at 08:39
  • If you looked through any of the numerous questions on SO about this you should have seen that you gennerally donät use a regexp for these kinds of tasks. Get yourself a proper XML-parser instead! – Fredrik Pihl Aug 30 '11 at 08:39

1 Answers1

2

If it has to be a regex, and if there is only one CMEngine tag per string:

Pattern regex = Pattern.compile("(?<=<CMEngine>)(?:(?!</CMEngine>).)*", Pattern.DOTALL);
Matcher regexMatcher = regex.matcher(subjectString);
if (regexMatcher.find()) {
    ResultString = regexMatcher.group();
}

Since that output appears to be machine-generated and is unlikely to contain comments or other stuff that might confuse the regex, this should work quite reliably.

It starts at a position right after a <CMEngine> tag: (?<=<CMEngine>)
and matches all characters until the next </CMEngine> tag: (?:(?!</CMEngine>).)*.

Tim Pietzcker
  • 328,213
  • 58
  • 503
  • 561