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>
2
  • not sure what you'd like to achieve but see if this can help: stackoverflow.com/questions/7008466/selecting-xml-raw-text Commented Aug 30, 2011 at 8: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! Commented Aug 30, 2011 at 8:39

1 Answer 1

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>).)*.

Sign up to request clarification or add additional context in comments.

6 Comments

I tested it here but did not give me what I wanted. Also I don't require java code, just provide a regex in the location
@Abishek: Of course it didn't work, you didn't set the DOTALL option.
Oh Thanks. my bad. What does DOTALL means?
@Abishek: "Dot matches all", i. e. also newlines.
Hi can you please take a look at a similar question stackoverflow.com/questions/7280740/…
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.