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
Revered Legend

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
Revered Legend

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!

Extending Observability Content to Splunk Cloud

Watch Now!   In this Extending Observability Content to Splunk Cloud Tech Talk, you'll see how to leverage ...

More Control Over Your Monitoring Costs with Archived Metrics GA in US-AWS!

What if there was a way you could keep all the metrics data you need while saving on storage costs?This is now ...

New in Observability Cloud - Explicit Bucket Histograms

Splunk introduces native support for histograms as a metric data type within Observability Cloud with Explicit ...