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!

Troubleshooting the OpenTelemetry Collector

  In this tech talk, you’ll learn how to troubleshoot the OpenTelemetry collector - from checking the ...

Adoption of Infrastructure Monitoring at Splunk

  Splunk's Growth Engineering team showcases one of their first Splunk product adoption-Splunk Infrastructure ...

Modern way of developing distributed application using OTel

Recently, I had the opportunity to work on a complex microservice using Spring boot and Quarkus to develop a ...