Splunk Search
Highlighted

understanding regex better

Motivator

I am trying to understand better how splunk regex works.

I have the below example:

This is a sample of the data I am trying to extract with Slot No.=1 being of interest:
...,measObjLdn="SGSN02KPR/Process:Process No.=5, Process type=GBP, Slot No.=1,...

Using splunks Interactive field extractor gives me this
(?i)=.*?, (?P\w+\s+\w+\.=\d+)(?=,)

if I want to look at these values in the search I can do this
`... | rex "(?i)=.*?, (?P\w+\s+\w+.=\d+)(?=,)" | stats values(FIELDNAME)

Now to understand this better I would like to use an example. What if I just want the values so instead of getting
Slot No.=1 I would get 1

So this (?i)=.*?, (?P\w+\s+\w+\.=\d+)(?=,) would go to ...?

Appreciate any help and trying to understand this better, or is there any documentation on this.

EDIT1
For instance this | rex ".*,.*Slot No.=(?P[^,]+)" will give me just the values this is just me playing around, I don't fully understand it.

Tags (2)
0 Karma
Highlighted

Re: understanding regex better

Splunk Employee
Splunk Employee

Perhaps for you, its better to do a rex as follows, as you mentioned:

search.. | rex field=_raw "Slot No.\=(?<slot_number>[^,]+),"

This isnt a proper forum to learn regex, but this matches a "Slot No.=" string, and then uses a capture group for any thing that isnt a "," repeated infinite times till it finds a ",".

Key take away here is regex capture groups, and pattern matching.

View solution in original post

0 Karma
Highlighted

Re: understanding regex better

Splunk Employee
Splunk Employee

Regex, or regular expressions, are not a idea in only in the world of Splunk. These are used throughout the development world and have been for years. I would recommend you study a bit on PCRE based patterns to get a better understanding of how regular expressions fit into the world of Splunk.

www.regex101.com is quite a valuable resource, not to mention others out there..

0 Karma
Highlighted

Re: understanding regex better

Motivator

tks but would you help me understand and break this down?

"(?i)=.*?, (?P\w+\s+\w+.=\d+)(?=,)"

have to rewrite the above as everything between < and > symbols disappears, but not if i use &lt and &gt
"(?i)=.*?, (?P&ltFIELDNAME&gt\w+\s+\w+.=\d+)(?=,)"

&lt = <
&gt = >

&lt = <
&gt = >

0 Karma
Highlighted

Re: understanding regex better

Motivator

tks you have giving me food for thought!! but what is a good forum for regex is it stackoverflow?

0 Karma
Highlighted

Re: understanding regex better

Champion

You can learn regex as you go I believe, you only really need help in truly complicated cases. What helps a lot is a tool like regex101 where you can try your expression on your sample data and which highlights your syntax graphically, explains your regex step for step and also has a quick reference of frequently used commands at hand - it's really great!

If you DO post regex code here, please keep in mind to post it as code - not with grave accents, but with an empty row before it and indented by four spaces. That will preserve any special characters such as angular brackets, which, as you can see, are lost in the question you posted.

0 Karma
Highlighted

Re: understanding regex better

Motivator

tks Jeff

Below posted using the code button above (

 )

"(?i)=.*?, (?P<FIELDNAME>w+s+w+.=d+)(?=,)"

Below posted using grave accents - Note the difference

"(?i)=.*?, (?Pw+s+w+.=d+)(?=,)"

0 Karma
Highlighted

Re: understanding regex better

Community Manager
Community Manager

Hi @HattrickNZ

Just thought you might find this useful, but here's a previous answers post where a handful of users on Answers shared their favorite online (and 1 offline) regex resources. One of them has exercises for practice and better understanding.
http://answers.splunk.com/answers/153171/is-there-any-online-regex-tool-to-create-regular-e.html

0 Karma