Regex in Your SPL: An Easy Introduction
Regex in Your SPL: An Easy Introduction
Regex in Your SPL: An Easy Introduction
An Easy Introduction
The forward-looking statements made in this presentation are being made as of the time and date of its live
presentation. If reviewed after its live presentation, this presentation may not contain current or accurate
information. We do not assume any obligation to update any forward looking statements we may make. In
addition, any information about our roadmap outlines our general product direction and is subject to change
at any time without notice. It is for informational purposes only and shall not be incorporated into any contract
or other commitment. Splunk undertakes no obligation either to develop the features or functionality
described or to include any such feature or functionality in a future release.
Splunk, Splunk>, Listen to Your Data, The Engine for Machine Data, Splunk Cloud, Splunk Light and SPL are trademarks and registered trademarks of Splunk Inc. in
the United States and other countries. All other brand names, product names, or trademarks belong to their respective owners. © 2017 Splunk Inc. All rights reserved.
Basics of Regular
Expressions
What is this Regex thing all about?
© 2017 SPLUNK INC.
Matching String: 22 Aug 2017 18:45:20 On this date, Michael made BBQ references
Regex Basics
To Protect and Give Options
Control Characters: Character Types: Protection Characters:
^ Start of a Line \s White Space \ The next character is a literal
$ End of a Line \S Not white space
\d Digit
Special Characters: \D Not Digit
| Alternative / “or” \w Word Character
\W Not Word Characters
server:253fsf2,host=23423
Keep going so long as
server: 253fsf2,host=23423
you hit
server:253f sf2,host=23423
characters that are
lowercase a-Z or 0-9
Go until you hit a space
Regex Basics
Say What Again
Control Characters: Character Types: Protection Characters:
^ Start of a Line \s White Space \ The next character is a literal
$ End of a Line \S Not white space
\d Digit
Inclusion Characters: Repetition:
Special Characters: \D Not Digit
[] Include {#} Number of Repetitions
| Alternative / “or” \w Word Character
[^] Exclude {#,#} Range of Repetitions
\W Not Word Characters
Regex: user:\s(?<username>[^@]+)
Go until we hit an @
Log 1: blah blah user: [email protected] Capture as field username
Log 2: more blah user: [email protected] Anchor off user:\s
Regex in SPL
Using Regular Expressions to improve your SPL
Regex in Your SPL
Search Time Regex
Field Extractions
On the fly (No need to work ahead)
erex Command
Field Extractions Using Examples
Use Splunk to generate regular expressions by providing a list of values from the data.
Easter egg that New Field to create Examples from the data
creates sample data
erex Command
Field Extractions Using Examples
Front Anchor
Named Field Extraction Grab any non-space character
rex Command
Extract Fields Using Regular Expressions at Search Time
Result:
…
Set the mode | rex mode=sed "s/Old
(Norse)/Not-so-old \1/g"
s for substitute
Evaluation
Using Regular Expressions for Pattern Matching
Regex Command
Filter Using Regular Expressions
match(SUBJECT),”REGEX”
… | eval n = if(match(field,”^MyRegex”, 1, 2)
sourcetype=access_combined_wcookie
| eval com = if(match(referer,"http:.*\.com"),"True","False")
operator operator
Persistence
Regular Expressions That Exist Outside Your Search
Until this point, every one of our extractions have only existed
in the search. But, what if we want them to persist? Or to share them?
– You may want to rewrite the – Requires Admin Rights (or an – Invoked by props.conf
generated Regex admin to put in place)
– Requires Admin Rights (or an
– Does not require admin rights admin to put in place)
Q&A
Michael Simko | Sr. Engineer/Instructor
© 2017 SPLUNK INC.
Thank You
Don't forget to rate this session in the
.conf2017 mobile app
Appendix A
Caveats
rex Command – Caveat
Use Rex to Perform SED Style Substitutions
Practice: Create a Regex that describes all three of the following strings
A solution:
\d+\s\w+\s\d+\s\d*\.\d*\.\d*\.\d*
Try it, then click the down chevron to see the results