Splunk Search

How to regex specific word string?

limalbert
Path Finder

Hello,

Ignoring commas and spaces, how do I grab just the name string from the below log? Below regex kept returning the value of FirstName. It's not seeing name as "Name=", but as "*Name=*".

Regex:

 

"Name=(?<nameCaptured>[^\,]*)"

 

Log:

 

FirstName=Hello, LastName=World, Name=HelloWorld, Address=NoAddress

 

Labels (1)
0 Karma
1 Solution

richgalloway
SplunkTrust
SplunkTrust

Try \W in place of \b.  If that doesn't work then please share the full set of requirements as well as more complete sample events so we can offer better solutions.

---
If this reply helps you, Karma would be appreciated.

View solution in original post

0 Karma

richgalloway
SplunkTrust
SplunkTrust

There is an implicit '.*' at the beginning of every regex unless it starts with '^'.  To get a string and not a substring the regex must uniquely identify the string to find.  Try one of these:

"\bName=(?<nameCaptured>[^\,]*)"
", Name=(?<nameCaptured>[^\,]*)"
---
If this reply helps you, Karma would be appreciated.
0 Karma

limalbert
Path Finder

Adding "\b" before Name doesn't do the trick. By adding a comma and space, it will only capture data in that format. 

Unfortunately, I notice some logs may or may not have a comma and a space.  But the string "Version=" is definitely there.

 

0 Karma

richgalloway
SplunkTrust
SplunkTrust

Try \W in place of \b.  If that doesn't work then please share the full set of requirements as well as more complete sample events so we can offer better solutions.

---
If this reply helps you, Karma would be appreciated.
0 Karma

manjunathmeti
Champion

hi @limalbert ,

Actually, your regex is matching 3 groups and the first matched value is returned to the field nameCaptured.  If you set max_match=0 in rex command you'll see all the matches,

 

| makeresults 
| eval _raw="FirstName=Hello, LastName=World, Name=HelloWorld, Address=NoAddress" 
| rex max_match=0 "Name=(?<nameCaptured>[^\,]*)" 

 

You can get the value of Name as below:

 

| makeresults 
| eval _raw="FirstName=Hello, LastName=World, Name=HelloWorld, Address=NoAddress" 
| rex max_match=0 "Name=(?<nameCaptured>[^\,]*)" 
| eval NameCaptured=mvindex(nameCaptured, 2)

 

Or change regex to match only Name=,

 

| makeresults 
| eval _raw="FirstName=Hello, LastName=World, Name=HelloWorld, Address=NoAddress" 
| rex "\W+Name=(?<nameCaptured>[^\,]*)"

 

 

If this reply helps you, an upvote/like would be appreciated.

0 Karma
Get Updates on the Splunk Community!

Index This | I’m short for "configuration file.” What am I?

May 2024 Edition Hayyy Splunk Education Enthusiasts and the Eternally Curious!  We’re back with a Special ...

New Articles from Academic Learning Partners, Help Expand Lantern’s Use Case Library, ...

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

Your Guide to SPL2 at .conf24!

So, you’re headed to .conf24? You’re in for a good time. Las Vegas weather is just *chef’s kiss* beautiful in ...