Splunk Search

Some RegEx help with date formatting

efelder0
Communicator

I am extracting a date/time stamp out of some XML; however, I need to strip out the time from the string.

i.e. - 3/7/2012 2:25:52 PM (GMT) --> needs to be: 3/7/2012

What would the REGEX be and would that regex be in the props.conf or can it go in the search string?

Here is the entry in props = EXTRACT-CreateTimeStamp_GMT = (?i)(?P[^<]+)

Tags (1)
0 Karma

kristian_kolb
Ultra Champion

If your XML looks something like this;

<blaha>BLAHA</blaha>
<createtimestampgmt>3/7/2012 2:25:52 PM (GMT)</createtimestampgmt>
<blaha2>BLAHA2</blaha2>

the following props.conf extract would get you the date only

EXTRACT-DateFromTimeStamp = (?i)<createtimestampgmt>(?P<createtimestamp_gmt>[^\s]+)

i.e. read everything from the end of the start tag up to the first whitespace character, and save it as (the somewhat odd name) createtimestamp_gmt

Hope this helps,

Kristian

0 Karma

cvajs
Contributor

efelder0,
what do you mean by "date". date as in "MM/DD/YYYY" or date as "MM/DD/YYYY HH:MM:SS (AM|PM)"

i would opt to use [\d]{2}/[\d]{2}/[\d]{4} to grab MM/DD/YYYY just in case the space comes up missing (not likely, but you never know)

0 Karma

kristian_kolb
Ultra Champion

The REGEX would be the same, i.e.

[^\s]+

if your event text contains square brackets, you need to escape them, otherwise they will be treated as part of the regex, i.e. \[CDATA\[

Please mark the question as answered if this solved your problem. Thanks.

Kristian

0 Karma

efelder0
Communicator

Yes, that worked. However, I have another field that requires reformatting. But, this time I am doing my field extraction from the transforms.conf. For example, I am pulling a field called "IncidentTime" and its value is 12/11/2011 11:16:48 PM. Here is the entry in transforms.conf:
REGEX = (?i)<![CDATA[(?P[a-zA-Z -:\d]+)(?=])

What would the new REGEX be to only include the date string? or would it be better to use the strptime function from within the search?

0 Karma

cvajs
Contributor

not really sure what you are wanting
[\d]{1,2}/[\d]{1,2}/[\d]{4} will grab 3/7/2012 or 12/12/2012 , etc.

0 Karma
Get Updates on the Splunk Community!

Unlock Database Monitoring with Splunk Observability Cloud

  In today’s fast-paced digital landscape, even minor database slowdowns can disrupt user experiences and ...

Purpose in Action: How Splunk Is Helping Power an Inclusive Future for All

At Cisco, purpose isn’t a tagline—it’s a commitment. Cisco’s FY25 Purpose Report outlines how the company is ...

[Upcoming Webinar] Demo Day: Transforming IT Operations with Splunk

Join us for a live Demo Day at the Cisco Store on January 21st 10:00am - 11:00am PST In the fast-paced world ...