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!

What You Read The Most: Splunk Lantern’s Most Popular Articles!

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

See your relevant APM services, dashboards, and alerts in one place with the updated ...

As a Splunk Observability user, you have a lot of data you have to manage, prioritize, and troubleshoot on a ...

Index This | What goes away as soon as you talk about it?

May 2025 Edition Hayyy Splunk Education Enthusiasts and the Eternally Curious!  We’re back with this month’s ...