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!

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 ...

They're back! Join the SplunkTrust and MVP at .conf24

With our highly anticipated annual conference, .conf, comes the fez-wearers you can trust! The SplunkTrust, as ...

Enterprise Security Content Update (ESCU) | New Releases

Last month, the Splunk Threat Research Team had two releases of new security content via the Enterprise ...