Splunk Search

Regex : Extract text between first and second semi-colon from the right

mdeterville
Path Finder

Hello SMEs:

I need some assistance extracting everything between the 1st and 2nd semi-colon ; (FROM THE RIGHT)  from a string like this: SITES;Bypass;Whitelist;Finance;User Business Accept

In this case, the output would be Finance.

Note: text between the semi-colon's may change 

Any assistance would be appreciated.

Regards,

Mac

Labels (1)
0 Karma
1 Solution

tscroggins
Influencer

@mdeterville 

The * quantifier is greedy by default. This should work:

| rex ".*;(?<value>.*);"

Try it here.

View solution in original post

mdeterville
Path Finder

@tscroggins  Ran into an issue where the data set also includes some categories only contains 1 Semi colon, similar to: "Business/Economy;User Accepted Business".

Any idea if OR conditions can be used to combine the initial solution and another solution that grabs everything before the first semi-colon (FROM THE LEFT) when it finds fields with only one semi colon?

Thanks in advance.

-Mac

0 Karma

tscroggins
Influencer

@mdeterville

There are many (infinite!) ways to construct regular expressions. To capture everything between the first semicolon and either the second semicolon or the end of the line, you can use e.g.:

| rex ".*?;(?<value>[^;$]+);?"

$ is an anchor (a special token) representing the end of the string. The construct [^;$]+ means one or more characters not matching semicolon or end of string. The capture will stop when it encounters either of those characters.

You can plug the regular expression into regex101.com or another tool to test it with a step by step explanation.

0 Karma

tscroggins
Influencer

@mdeterville 

The * quantifier is greedy by default. This should work:

| rex ".*;(?<value>.*);"

Try it here.

mdeterville
Path Finder

@tscroggins Worked like a charm - Thanks for the quick response!

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 ...