Splunk Search

Regex for extracting a xml node from a xml feild

ssekar
Engager

Hello Expert,

Please help me arrive on a regex to extract a xml node in a xml field.

I have a field value like below

<Reponse status="failure">
 <messages>
        <message id="Payload">
            <UpdateAccountRq>
                <AccountId>123465</AccountId>
                <NewStatus>Active</NewStatus>
            </UpdateAccountRq>
        </message>
    </messages>
</Reponse>
 
And I want to extract the below xml node and display it in a separate field.
 
 <UpdateAccountRq>
        <AccountId>123465</AccountId>
       <NewStatus>Active</NewStatus>
</UpdateAccountRq>
 
I tried many ways, but nothing works.
 
Attempt 1:  rex field=Action "messages>(?<Payload>.+)<\/messages" | table Action, Payload
Attempt 2:  rex field=Action "\<message id=\"Payload\">(?<Payload>[^<\/message]+)" | table Action, Payload
 
Please help. Thanks
Labels (2)
0 Karma
1 Solution

richgalloway
SplunkTrust
SplunkTrust

@yuanliu is correct, but for a pure rex solution, try this regex

\<message id=\"Payload\">(?<Payload>[\s\S]+?)\<\/message

 

---
If this reply helps you, Karma would be appreciated.

View solution in original post

ssekar
Engager

Thanks for the help. All three are good answers. 

0 Karma

ITWhisperer
SplunkTrust
SplunkTrust
| rex field=Action "(?ms)messages>(?<Payload>.+)<\/messages"

richgalloway
SplunkTrust
SplunkTrust

@yuanliu is correct, but for a pure rex solution, try this regex

\<message id=\"Payload\">(?<Payload>[\s\S]+?)\<\/message

 

---
If this reply helps you, Karma would be appreciated.

yuanliu
SplunkTrust
SplunkTrust

If your document is conformant XML, you should use builtin commands such as spath; regex will be difficult to maintain if the format, or syntax, or schema change.  In your case, assuming you have a field named "xml", you can do

 

| spath input=xml path=Reponse.messages.message.UpdateAccountRq

 

Your sample data gives the following

Reponse.messages.message.UpdateAccountRqxml
<AccountId>123465</AccountId> <NewStatus>Active</NewStatus><Reponse status="failure"> <messages> <message id="Payload"> <UpdateAccountRq> <AccountId>123465</AccountId> <NewStatus>Active</NewStatus> </UpdateAccountRq> </message> </messages> </Reponse>

 

Tags (1)
Get Updates on the Splunk Community!

Share Your Ideas & Meet the Lantern team at .Conf! Plus All of This Month’s New ...

Splunk Lantern is Splunk’s customer success center that provides advice from Splunk experts on valuable data ...

Combine Multiline Logs into a Single Event with SOCK: a Step-by-Step Guide for ...

Combine multiline logs into a single event with SOCK - a step-by-step guide for newbies Olga Malita The ...

Stay Connected: Your Guide to May Tech Talks, Office Hours, and Webinars!

Take a look below to explore our upcoming Community Office Hours, Tech Talks, and Webinars this month. This ...