Splunk Search

How to extract seven digit number with no special characters or white space before/after?

rhenry
Explorer

Hello,

I am attempting to extract from a field a seven digit number which can sometimes have a space or special character such as # in front of it. I want to be able to output it such that the new field only returns the seven digit number, no special characters or white space before and after. Also, I want to set it such that it will exclude where the seven digit number begins with zero. So far, I have only been able to come up with and tried the following in regular expression:

(?<Field1>\d\d\d\d\d\d\d) *Pulls less than seven digits as well; need exactly seven.

(?<Field1>[^a-zA-Z]\d{7}) *Does not omit special characters before it and pulls seven digit numbers of 0000000 (want to exclude these).

Can I get some assistance on what the correct regular expression is to be able to pull a seven digit number with no special characters or space before/after and not all zeroes? Thanks!

Labels (5)
0 Karma
1 Solution

gcusello
SplunkTrust
SplunkTrust

Hi @rhenry,

if you always have "#" before the number, you could use something like this:

| rex "\#(?<ID>\d{7})"

that you can test at https://regex101.com/r/aQEFp4/1

if there could be other conditions, please share some example.

Ciao.

Giuseppe

View solution in original post

0 Karma

ITWhisperer
SplunkTrust
SplunkTrust

Does this work for you?

(?<Field1>[1-9]\d{6})

gcusello
SplunkTrust
SplunkTrust

Hi @rhenry,

could you share some sample of your logs, highlighting the part of logs to take.

Ciao.

Giuseppe

0 Karma

rhenry
Explorer

I can give an example:

“The analysis of the log shows for website http://www.somewebsite.com/url-id/1234567abc124def343 there was a malicious attack, ID #1234567.”

In the example above, I only want to pull the second number with ID in front of it. I do not want to pull the seven digit number in the url above. Does that make sense?

0 Karma

gcusello
SplunkTrust
SplunkTrust

Hi @rhenry,

if you always have "#" before the number, you could use something like this:

| rex "\#(?<ID>\d{7})"

that you can test at https://regex101.com/r/aQEFp4/1

if there could be other conditions, please share some example.

Ciao.

Giuseppe

0 Karma

rhenry
Explorer

This is helpful.

So, drilling down on the first example, I want to be be able to specifically locate where it begins with "ID" and then capture that seven digit number afterward. I might have some cases where a different word would appear before it (i.e. "Investigation" or "Incident"). Is it possible to write a regular expression where it will look for multiple words and capture the seven digit number following it? Thanks!

0 Karma

gcusello
SplunkTrust
SplunkTrust

Hi @rhenry,

yes it's possible, but to help you I need some examples of the various logs that's possible to parse.

Ciao.

Giuseppe

0 Karma

rhenry
Explorer

I think I have found the solution to my problem. Thanks!

0 Karma

gcusello
SplunkTrust
SplunkTrust

Hi @rhenry,

good for you, see next time!

Ciao and happy splunking

Giuseppe

P.S.: Karma Points are appreciated by all the Contyributors  😉

0 Karma

ITWhisperer
SplunkTrust
SplunkTrust
(incident\s|investigation\s|ID\s\#)(?<ID>[1-9]\d\d\d\d\d\d)
Career Survey
First 500 qualified respondents will receive a $20 gift card! Tell us about your professional Splunk journey.

Can’t make it to .conf25? Join us online!

Get Updates on the Splunk Community!

What Is Splunk? Here’s What You Can Do with Splunk

Hey Splunk Community, we know you know Splunk. You likely leverage its unparalleled ability to ingest, index, ...

Level Up Your .conf25: Splunk Arcade Comes to Boston

With .conf25 right around the corner in Boston, there’s a lot to look forward to — inspiring keynotes, ...

Manual Instrumentation with Splunk Observability Cloud: How to Instrument Frontend ...

Although it might seem daunting, as we’ve seen in this series, manual instrumentation can be straightforward ...