Dashboards & Visualizations

classic xml - token eval using replace function not working

_guy
Explorer


So I've been turning myself inside out trying to figure this one out and cannot...

In search this works fine, 'test' evaluates to "default"

| makeresults
| eval value = "users|default"
| eval test = replace(value, "(\w*)\|(\w*)", "\2")

Trying to make this work as a token eval however is proving difficult. I am trying to do this:
(where <set token="form.chg_focus">users|default</set> is defined elsewhere to trigger a change for the input)

<input type="text" token="chg_focus" depends="$HIDDEN$">
  <change>
    <eval token="nav_chart_mode">replace($value$, "(\w*)\|(\w*)", "\2")</eval>
  </change>
</input>


The above is a simplified version of the input I am trying to implement which involves multiple token sets and evals.

I figured out that I needed to double up the instances of \ in the regex expression , so for example this works (i.e. I get $nav_chart_mode$=default)

<eval token="nav_chart_mode">replace($value$, "\\w*\\|", "")</eval>


So does this 

<eval token="nav_chart_mode">replace($value$, "\\w*\\|\\w*", "default")</eval>


However I cannot seem to get this to work - all I get is $nav_chart_mode$="" (i.e. blank string). 

<eval token="nav_chart_mode">replace($value$, "(\\w*)\\|(\\w*)", "\2")</eval>


Using "\\2" results in $nav_chart_mode$=\2, so that isn't the answer.

I also tried using \( and \), i.e. "\(\\w*\)\\|\(\\w*\)", and no joy there - also results in $nav_chart_mode$="".

I also tried using double (( and )), also not helpful... (and not logical, but I was getting desperate 😊)

I am hoping someone out there can help me understand what I am not understanding...

Cheers

 

Labels (1)
0 Karma
1 Solution

ITWhisperer
SplunkTrust
SplunkTrust

Different flavours of regex use different ways to reference captured values - it looks like Splunk may be using different flavours between SPL and token evals. Try this

<eval token="nav_chart_mode">replace($value$, "(\\w*)\\|(\\w*)", "$2")</eval>

View solution in original post

richgalloway
SplunkTrust
SplunkTrust

You tried doubling the escape characters in the pattern, but did you try it in the replacement?

<eval token="nav_chart_mode">replace($value$, "(\\w*)\\|(\\w*)", "\\2")</eval>
---
If this reply helps you, Karma would be appreciated.
0 Karma

_guy
Explorer

Thanks for the suggestion richgalloway, but yes, I tried that and it resulted in this

nav_chart_mode="\2"

 

0 Karma

ITWhisperer
SplunkTrust
SplunkTrust

Different flavours of regex use different ways to reference captured values - it looks like Splunk may be using different flavours between SPL and token evals. Try this

<eval token="nav_chart_mode">replace($value$, "(\\w*)\\|(\\w*)", "$2")</eval>

_guy
Explorer

Awesome, thank you! I felt it had to be possible with the right tweak, and you nailed it 🙏

0 Karma
Get Updates on the Splunk Community!

AI for AppInspect

We’re excited to announce two new updates to AppInspect designed to save you time and make the app approval ...

App Platform's 2025 Year in Review: A Year of Innovation, Growth, and Community

As we step into 2026, it’s the perfect moment to reflect on what an extraordinary year 2025 was for the Splunk ...

Operationalizing Entity Risk Score with Enterprise Security 8.3+

Overview Enterprise Security 8.3 introduces a powerful new feature called “Entity Risk Scoring” (ERS) for ...