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!

Index This | I am a number, but when you add ‘G’ to me, I go away. What number am I?

March 2024 Edition Hayyy Splunk Education Enthusiasts and the Eternally Curious!  We’re back with another ...

What’s New in Splunk App for PCI Compliance 5.3.1?

The Splunk App for PCI Compliance allows customers to extend the power of their existing Splunk solution with ...

Extending Observability Content to Splunk Cloud

Register to join us !   In this Extending Observability Content to Splunk Cloud Tech Talk, you'll see how to ...