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!

Database Performance Sidebar Panel Now on APM Database Query Performance & Service ...

We’ve streamlined the troubleshooting experience for database-related service issues by adding a database ...

IM Landing Page Filter - Now Available

We’ve added the capability for you to filter across the summary details on the main Infrastructure Monitoring ...

Dynamic Links from Alerts to IM Navigators - New in Observability Cloud

Splunk continues to improve the troubleshooting experience in Observability Cloud with this latest enhancement ...