Splunk Search

I would like to run a regex search to find two words no more the six words apart

BrandSentiment
Explorer

Is this string anywhere near where I need to be to find word1 and word2 no more than 6 words apart in the field interaction.content:

interaction.content word1\W+(?:\w+\W+){1,6}?word2

Any help much appreciated

Tags (2)
1 Solution

BrandSentiment
Explorer

Hi Martin, Thanks for offering your help. By researching and playing with http://www.regexr.com/ I have found my answer and by working more with Regexr tool I have found answers to a lot more questions.

This is my solution:

index="main" interaction.type=twitter | regex interaction.content="((word1)\W+(?:\w+\W+){1,8}?(word2)W)" 

View solution in original post

thebarryk
Engager

The following regex may be more than you need, but has a few extra facets:

  1. Matches the two words in either order.
  2. Matches the words when they are adjacent.
  3. Uses word boundary anchors for matches at the beginning or end of the field.

    index="main" interaction.type=twitter | regex interaction.content="(?:\bword1\b\W(?:\w+\W+){0,6}?\bword2\b|\bword2\b\W(?:\w+\W+){0,6}?\bword1\b)"
    Hope it's useful.

Barry

BrandSentiment
Explorer

Hi Martin, Thanks for offering your help. By researching and playing with http://www.regexr.com/ I have found my answer and by working more with Regexr tool I have found answers to a lot more questions.

This is my solution:

index="main" interaction.type=twitter | regex interaction.content="((word1)\W+(?:\w+\W+){1,8}?(word2)W)" 

BrandSentiment
Explorer

Correction - for some reason the backslash did't show above:

index="barclays" interaction.type=twitter | regex interaction.content="((word1)\W+(?:\w+\W+){1,8}?(word2)\W)"

martin_mueller
SplunkTrust
SplunkTrust

How is "word" defined in your context?

Got Sample data?

What's not working in your own solution?

0 Karma
Get Updates on the Splunk Community!

Index This | I am a number, but when you add ‘G’ to me, I go away. What number am I?

March 2024 Edition Hayyy Splunk Education Enthusiasts and the Eternally Curious!  We’re back with another ...

What’s New in Splunk App for PCI Compliance 5.3.1?

The Splunk App for PCI Compliance allows customers to extend the power of their existing Splunk solution with ...

Extending Observability Content to Splunk Cloud

Register to join us !   In this Extending Observability Content to Splunk Cloud Tech Talk, you'll see how to ...