A regular expression primer

Most of the ways to create new fields in Splunk involve regular expressions (sometimes referred to as regex). As mentioned in the Splunk documentation:

"Regex is a powerful part of the Splunk search interface, and understanding it is an essential component of Splunk search best practices"

There are many books and sites dedicated to regular expressions, so we will only touch upon the subject here. The following examples are really provided for completeness; the Splunk web interface may suffice for most users.

Given the log snippet ip=1.2.3.4, let's pull out the subnet (1.2.3) into a new field called subnet. The simplest pattern would be the following literal string:

ip=(?P<subnet>1.2.3).4 

This is not terribly useful as it will only find the subnet of that one IP address. Let's try a slightly more complicated example:

ip=(?P<subnet>\d+\.\d+\.\d+)\.\d+ 

Let's step through this pattern:

Now, let's step through an overly complicated pattern to illustrate a few more concepts:

ip=(?P<subnet>\d+.\d*\.[01234-9]+)\.\d+ 

Let's step through this pattern:

There are a number of different ways to accomplish the task at hand. Here are a few examples that will work:

For more information about regular expressions, consult the manual pages for Perl Compatible Regular Expressions (PCRE), which can be found online at http://www.pcre.org/pcre.txt, or one of the many regular expression books or websites dedicated to the subject. We will build more expressions as we work through different configurations and searches, but it's definitely worthwhile to have a reference handy.