Getting Data In

New source type with regex not working correctly

tchamp
Explorer

I've created a new source type with a regex. It was working but I found an edge case where it was broken. I rewrote the regex using a capture group but the group doesn't seem to be  getting applied. Can someone tell me if this should work?

Here is my regex: s/"message":\s*"{([\s\S]*)}"/"data": {$1}/g put in "SEDCMD-a". 

My json data is as follows:

{
  "message": "{
      "test": "test data"
  }"
}

and my transformed data ends up like this:

{  "data": {$1}}

It isn't making the replacement with the capture group.

Am I doing something wrong? Should this work?

Thanks,

-Tim

Labels (2)
0 Karma
1 Solution

livehybrid
Super Champion

Hi @tchamp 

I think you need to replace the $1 capture group with a \1 

Here is an example in SPL 

livehybrid_0-1742248659468.png

| makeresults 
|eval _raw="{
  \"message\": \"{
      \"test\": \"test data\"
  }\"
}
"
|rex mode=sed "s/\"message\":\s*\"{([\s\S]*)}\"/\"data\": {\1}/g"

Please let me know how you get on and consider adding karma to this or any other answer if it has helped.
Regards

Will

 

View solution in original post

livehybrid
Super Champion

Hi @tchamp 

Check out my other response first, but you may also be able to achieve this with an INGEST_EVAL

== props.conf ==
[yourSourceType]
TRANSFORMS-extractMessage = myExtract

== transforms.comf ==
[myExtract]
INGEST_EVAL = _raw:=json_extract(_raw,"message")

Visualised as SPL this would be

livehybrid_0-1742248935015.png

Please let me know how you get on and consider adding karma to this or any other answer if it has helped.
Regards

Will

 

0 Karma

livehybrid
Super Champion

Hi @tchamp 

I think you need to replace the $1 capture group with a \1 

Here is an example in SPL 

livehybrid_0-1742248659468.png

| makeresults 
|eval _raw="{
  \"message\": \"{
      \"test\": \"test data\"
  }\"
}
"
|rex mode=sed "s/\"message\":\s*\"{([\s\S]*)}\"/\"data\": {\1}/g"

Please let me know how you get on and consider adding karma to this or any other answer if it has helped.
Regards

Will

 

tchamp
Explorer

This was the answer. I appreciate the help.

0 Karma
Get Updates on the Splunk Community!

Brains, Bytes, and Boston: Learn from the Best at .conf25

When you think of Boston, you might picture colonial charm, world-class universities, or even the crack of a ...

Splunk AppDynamics Agents Webinar Series

Mark your calendars! On June 24th at 12PM PST, we’re going live with the second session of our Splunk ...

SplunkTrust Application Period is Officially OPEN!

It's that time, folks! The application/nomination period for the 2025 SplunkTrust is officially open! If you ...