Splunk Search

Regex in Field Transform not greedy?

Olli1919
Path Finder

Hi Base,

could it be that Regexes in Field Transforms are not greedy?

I am using this field transformation to extract sld.tld from hostnames:

[hostname_query_sub1]
CLEAN_KEYS = 1
MV_ADD = 0
SOURCE_KEY = querystring1
REGEX = ([^\.]+\.[^\.]+\.[^\.]+)
FORMAT = t2d::$1

Which gives these results:
1.2.3.4.in-addr.arpa -> 1.2.3
subdomain.subdomain.sld.tld -> subdomain.subdomain.sld

Using REGEX = ([^\.]+\.[^\.]+\.[^\.]+)$ works as intended:
1.2.3.4.in-addr.arpa -> 4.in-addr.arpa
subdomain.subdomain.sld.tld -> subdomain.sld.tld

Shouldn't the expression match to the end of the string even without "$"?

0 Karma
1 Solution

Ayn
Legend

It's working as you've instructed it to. The expression you've supplied will not match until the end of the string because you've explicitly specified that it should only match characters that are NOT a period ("."). The regex engine will return the earliest match it will find - this is not to be confused with whether the match itself is greedy or not, that's simply default regex engine operation. Greediness in matches is whether they will try to match as long as possible or not.

If you want the last three groups before the end of the string, you need to anchor the match at the end of the string, just like you've done in the last regex in your post.

View solution in original post

Ayn
Legend

It's working as you've instructed it to. The expression you've supplied will not match until the end of the string because you've explicitly specified that it should only match characters that are NOT a period ("."). The regex engine will return the earliest match it will find - this is not to be confused with whether the match itself is greedy or not, that's simply default regex engine operation. Greediness in matches is whether they will try to match as long as possible or not.

If you want the last three groups before the end of the string, you need to anchor the match at the end of the string, just like you've done in the last regex in your post.

Ayn
Legend

It's fast, I would write it in the same way 🙂

0 Karma

Olli1919
Path Finder

Thank you for the explation. So my first char class does not walk past the first dot - thanks. If I may follow-up on normal regex behavior: Ist this regex method (negate with anchor) fast, or could this be further optimized?

0 Karma
Get Updates on the Splunk Community!

[Puzzles] Solve, Learn, Repeat: Dynamic formatting from XML events

This challenge was first posted on Slack #puzzles channelFor a previous puzzle, I needed a set of fixed-length ...

Enter the Agentic Era with Splunk AI Assistant for SPL 1.4

  🚀 Your data just got a serious AI upgrade — are you ready? Say hello to the Agentic Era with the ...

Stronger Security with Federated Search for S3, GCP SQL & Australian Threat ...

Splunk Lantern is a Splunk customer success center that provides advice from Splunk experts on valuable data ...