Splunk Search

How to edit my regex to capture the Nth match in an XML file?

Estrellia
Explorer

Hello all,

I am struggling while trying to write a regex to capture the second and third occurrence of a pattern.

Here is my example:
The Following XML file for instance:

<Name>eth0</Name>
<IP>192.168.1.1/24</IP>
 <MACAddress>xx:xx:xx:xx:xx:x</MACAddress>
 <Status>ok</Status> 
<Name>eth1</Name>
 <IP>192.168.1.2/24</IP> 
<MACAddress>xx:xx:xx:xx:xx:x</MACAddress>
 <Status>ok</Status>

I manage to catch the first eth0 by doing:

<(Name)>(?<eth0name>\D+\d+)</(\1)>\n

But I can't manage to catch the eth1 field...
Sorry if this is a dumb question... I am new to Regex...
I am using https://regex101.com/ to help me out, but I'm stuck..

Can someone help me on this one please?
Thanks!

0 Karma
1 Solution

somesoni2
SplunkTrust
SplunkTrust

Will something like this work for you (run anywhere sample)

| gentimes start=-1 | eval _raw="<Name>eth0</Name> <IP>192.168.1.1/24</IP> <MACAddress>xx:xx:xx:xx:xx:x</MACAddress> <Status>ok</Status> <Name>eth1</Name> <IP>192.168.1.2/24</IP> <MACAddress>xx:xx:xx:xx:xx:x</MACAddress> <Status>ok</Status> <Name>eth3</Name> <IP>192.168.1.2/24</IP> <MACAddress>xx:xx:xx:xx:xx:x</MACAddress> <Status>ok</Status>" | table _raw 
| rex max_match=0 "(\<Name\>(?<Ethernet>[^\<]+)\<\/Name\>)" | eval instance1=mvindex(Ethernet,0) | eval instance2=mvindex(Ethernet,1) | eval instance3=mvindex(Ethernet,2)

View solution in original post

somesoni2
SplunkTrust
SplunkTrust

Will something like this work for you (run anywhere sample)

| gentimes start=-1 | eval _raw="<Name>eth0</Name> <IP>192.168.1.1/24</IP> <MACAddress>xx:xx:xx:xx:xx:x</MACAddress> <Status>ok</Status> <Name>eth1</Name> <IP>192.168.1.2/24</IP> <MACAddress>xx:xx:xx:xx:xx:x</MACAddress> <Status>ok</Status> <Name>eth3</Name> <IP>192.168.1.2/24</IP> <MACAddress>xx:xx:xx:xx:xx:x</MACAddress> <Status>ok</Status>" | table _raw 
| rex max_match=0 "(\<Name\>(?<Ethernet>[^\<]+)\<\/Name\>)" | eval instance1=mvindex(Ethernet,0) | eval instance2=mvindex(Ethernet,1) | eval instance3=mvindex(Ethernet,2)

Estrellia
Explorer

Hello,

Thank you for your help, this is indeed a way to achieve what I wanted to do.

I clearly didn't think of that.. thanks!

Have a nice day.

Ps: If anyone also think of a 100% pure regex way to do that I also am interested for learning purposes.

0 Karma
Get Updates on the Splunk Community!

Routing logs with Splunk OTel Collector for Kubernetes

The Splunk Distribution of the OpenTelemetry (OTel) Collector is a product that provides a way to ingest ...

Welcome to the Splunk Community!

(view in My Videos) We're so glad you're here! The Splunk Community is place to connect, learn, give back, and ...

Tech Talk | Elevating Digital Service Excellence: The Synergy of Splunk RUM & APM

Elevating Digital Service Excellence: The Synergy of Real User Monitoring and Application Performance ...