Splunk Search

rex stop after first match

poisar
Explorer

i have a field with several strings like

fieldname = AT-field2-field3

fieldname = DE-field2

fieldname = DE-field2-field3-field4

etc...

 

I try to get a rex to just get the country code:

|rex field=fieldname "^(?<country>.*)-.*"

 

but the result is not just the  Country Code

 

any ideas?

Labels (2)
0 Karma
1 Solution

richgalloway
SplunkTrust
SplunkTrust

The .* operator is greedy so it will grab as many characters as it can that still match the expression.  One solution is to use the non-greedy quantifier.

|rex field=fieldname "^(?<country>.*?)-.*"

Another solution is to take everything up to the first hyphen.  Like this:

| rex field=fieldname "^(?<country>[^-]+)-"

 

---
If this reply helps you, Karma would be appreciated.

View solution in original post

richgalloway
SplunkTrust
SplunkTrust

The .* operator is greedy so it will grab as many characters as it can that still match the expression.  One solution is to use the non-greedy quantifier.

|rex field=fieldname "^(?<country>.*?)-.*"

Another solution is to take everything up to the first hyphen.  Like this:

| rex field=fieldname "^(?<country>[^-]+)-"

 

---
If this reply helps you, Karma would be appreciated.

poisar
Explorer

thank you for your input. Both variants work and i learned something new 🙂

0 Karma
Get Updates on the Splunk Community!

Announcing Scheduled Export GA for Dashboard Studio

We're excited to announce the general availability of Scheduled Export for Dashboard Studio. Starting in ...

Extending Observability Content to Splunk Cloud

Watch Now!   In this Extending Observability Content to Splunk Cloud Tech Talk, you'll see how to leverage ...

More Control Over Your Monitoring Costs with Archived Metrics GA in US-AWS!

What if there was a way you could keep all the metrics data you need while saving on storage costs?This is now ...