I need a query that will tell me the count of a substring within a string like this ...
"This is my [string]" and I need find the word and count of [string]. "This is my" is always the same but [string] is dynamic and can be many things, such as apple, banana etc. I need tabular data returned to look like
Word Count
apple 3
I tried this but doesnt seem to working
rex field=_raw ".*This is my (?<string>\d+).*" | stats count by string
\d+ matches only digits, not any word.
If "This is my" is always constant, you can try below
rex field=_raw "This is my (?<string>\w+)" | stats count by string
Regards,
Prewin
Splunk Enthusiast | Always happy to help! If this answer helped you, please consider marking it as the solution or giving a kudos/Karma. Thanks!
Two problems with your regex.
A usable code to extract "apple" from "This is my [apple]" would be
| rex "This is my \[(?<string>[^\]]+)\]"
| stats count by string
Note:
In addition to the other comments, you don't need the .* at the start and end of the regex
Hi @caschmid
Would something like this work for you? This assumes you know the string you want count, is that right?
| rex max_match=100 field=_raw "(?<extract>\[string\])"
| stats count by extract
🌟 Did this answer help you? If so, please consider:
Your feedback encourages the volunteers in this community to continue contributing
Use https://regex101.com to verify your regexes.
In this case it won't work for "string" not being a number because \d+ means a sequence of digits. Depending on how precise you want to be with this match, you might want \S+ or some other variation.