Splunk Search

What does (?m) mean at the beginning of a regex

Justin_Grant
Contributor

I saw this in \etc\system\README\transforms.conf.example:

REGEX = (?m)^(.*)SessionId=\w+(\w{4}[&"].*)$

What does the (?m) mean before the caret? Is this really matching 0 or 1 "m" characters at the end of the previous line, or does it have some special meaning?

Tags (1)
1 Solution

Johnvey
Contributor

It declares the regex to read multiline data, i.e., don't stop the regex on a line break.

The (?<option_flag>) construct allows you to set various matching properties like case-insensitivity, multiline, greedy, etc. See http://www.regextester.com/pregsyntax.html for more info.

View solution in original post

gkanapathy
Splunk Employee
Splunk Employee

In general, all Splunk regexes use the PCRE flavor of regex, which is substantially the same regex syntax as Perl, Python, PHP, but significantly different from grep (or egrep).

Johnvey
Contributor

It declares the regex to read multiline data, i.e., don't stop the regex on a line break.

The (?<option_flag>) construct allows you to set various matching properties like case-insensitivity, multiline, greedy, etc. See http://www.regextester.com/pregsyntax.html for more info.

Jason
Motivator

More importantly, this "multiline mode" means that ^ and $ match the beginning and end (respectively) of each line, not the beginning and end of the entire string. This is important in multiline events, in case you want to find an item at the beginning of a line somewhere in your event.

landen99
Motivator

And just as importantly, rex will match against the entire event without (?m) even if there are line breaks. With max_match=0, rex will even match on the pattern multiple times in the same event thus creating a multi-value field.

0 Karma
Get Updates on the Splunk Community!

Built-in Service Level Objectives Management to Bridge the Gap Between Service & ...

Wednesday, May 29, 2024  |  11AM PST / 2PM ESTRegister now and join us to learn more about how you can ...

Get Your Exclusive Splunk Certified Cybersecurity Defense Engineer at Splunk .conf24 ...

We’re excited to announce a new Splunk certification exam being released at .conf24! If you’re headed to Vegas ...

Share Your Ideas & Meet the Lantern team at .Conf! Plus All of This Month’s New ...

Splunk Lantern is Splunk’s customer success center that provides advice from Splunk experts on valuable data ...