Splunk Search

Extracting words in a string with regular expressions

7ryota
Explorer

Hi,

i need help to extract word from a string

 

string

Security agent installation attempted Endpoint: (Not Found)
Security agent intstallation attempted Endpoint: hostname

 

result

Not Found

hostname

 

how can i construct a regular expression to extract out what i wanted?

 

Labels (1)
0 Karma
1 Solution

ITWhisperer
SplunkTrust
SplunkTrust
| rex ":\s+\(*(?<result>[^)]+)"

View solution in original post

0 Karma

inventsekar
SplunkTrust
SplunkTrust

not sure how to remove the ")" at the "Not Found)"

|makeresults | eval string="Security agent installation attempted Endpoint: (Not Found)
Security agent intstallation attempted Endpoint: hostname" 
| rex field=string max_match=0 ":\s+\(?(?P<result>.+)"
|table string result

rex-string.png

0 Karma

gcusello
SplunkTrust
SplunkTrust

Hi @7ryota,

you could use two regexes like the following:

| rex ":\s+\(*(?<result>.+)"
| rex field=result "^(?<result>[^)]+)"

The first extract the full value and the second deletes the parenthesis when present.

Ciao.

Giuseppe

0 Karma

ITWhisperer
SplunkTrust
SplunkTrust
| rex ":\s+\(*(?<result>[^)]+)"
0 Karma

inventsekar
SplunkTrust
SplunkTrust

Hi @ITWhisperer ... i am trying to learn and understand your rex, as i ran it, but it does not fetch the string.. not sure what went wrong.. please suggest. 

i used this search: 

|makeresults | eval string="Security agent installation attempted Endpoint: (Not Found) 
Security agent intstallation attempted Endpoint: hostname" 
| rex ":\s+\(*(?<result>[^)]+)"
|table string result

rex-string1.png

0 Karma

ITWhisperer
SplunkTrust
SplunkTrust

By default, rex operates on the _raw field. Either change your eval so it assigns to _raw rather than string or add field=string to the rex

inventsekar
SplunkTrust
SplunkTrust

Sure @ITWhisperer , but, still it found only first match.. the "hostname" was not matched.. 

rex-string2.png

0 Karma

ITWhisperer
SplunkTrust
SplunkTrust

You can use rex max_match=0 to get multiple matches

0 Karma
Get Updates on the Splunk Community!

Get the T-shirt to Prove You Survived Splunk University Bootcamp

As if Splunk University, in Las Vegas, in-person, with three days of bootcamps and labs weren’t enough, now ...

Introducing the Splunk Community Dashboard Challenge!

Welcome to Splunk Community Dashboard Challenge! This is your chance to showcase your skills in creating ...

Wondering How to Build Resiliency in the Cloud?

IT leaders are choosing Splunk Cloud as an ideal cloud transformation platform to drive business resilience,  ...