i am trying to reformate a field that i already extracted with a grok pattern.
By this i am willing to replace an extractor (replace with regex) that i am using.
rule "R-DeviceID Regex Replacement"
when
has_field("R-DeviceID")
then
let N-DeviceID = regex_replace(pattern: "(\d)(/\d)\/(\d+)\/(\d+)\:(\d+)\.(\d+)\.(\d+)",
value: to_string($message.R-DeviceID),
replace:"$3-$4-$5-$6-$7");
set_field = ("R-DeviceID", N-DeviceID);
end
Is something wrong with the Syntax i am using ?
The Value should be reformated from 1/1/01/02:2.1.1 to 01-02-2-1-1
I might be able to help. Just and FYI I’m not very good at pipelines /w regex but I did find some flaws.
It should look some thing like this.
rule "R-DeviceID Regex Replacement"
when
has_field("R-DeviceID")
then
let N-DeviceID = regex_replace("(\d)(/\d)\/(\d+)\/(\d+)\:(\d+)\.(\d+)\.(\d+),to_string($message.R-DeviceID), "$3-$4-$5-$6-$7");
set_field = ("R-DeviceID", N-DeviceID);
end
Next, I copy & paste you configuration above in Pipeline rules and there is an error with the regex section, Invalid expression.
rule "R-DeviceID Regex Replacement"
when
has_field("R-DeviceID")
then
let N-DeviceID = regex_replace("[^<>,\s]+(\d+)\:(\d+)\.(\d+)\.(\d+),to_string($message.R-DeviceID), "$3-$4-$5-$6-$7");
set_field = ("R-DeviceID", N-DeviceID);
end
Think were forgetting something but not sure what. @tmacgbay probably be our best bet to find out what this could be.
I found this post, it might help with the pipeline.
I just ran into something I totally forgot. There is a regex extractor called Replace with regular expression
As i mentioned in my first reply i already have an Extractor Replace with regular expression what i want to replace with a pipeline, because extractors use more system ressources than grok_pattern in a pipeline rule.
I tried so many Syntaxes for the regex_replace in a pipeline all i get is: Invalid Expression.
Yes i know about these and i am using so many Extrators but i am about to replace all of them.
I also tested the Regex on https://regex101.com/ by using Java Flavor because the regular expression to which the “value” string is to be matched; uses Java regex syntax.
rule "DHCPv6 R-DeviceID Regex Replacement"
when
has_field("R_DeviceID")
then
let N_DeviceID = regex_replace("%{deviceid}",to_string($message.R_DeviceID),"$3-$4-$5-$6-$7");
set_field("R_DeviceID", N_DeviceID);
end
i tried also
rule "DHCPv6 R_DeviceID Regex Replacement"
when
has_field("R_DeviceID")
then
let N_DeviceID = regex_replace(pattern: "%{deviceid}",value: to_string($message.R_DeviceID),replacement: "$3-$4-$5-$6-$7");
set_field("R_DeviceID", N_DeviceID);
end
I am getting this error message “Unable to pre-compute” value for 1st argument pattern in call to funtion regex_replace: Illegal repetition near index 0
set_field() is a function and doesn’t need an = sign so it would be:
set_field("R-DeviceID", the_result);
Regex in the pipeline needs double escapes (catches everyone)
Resulting non-complaining and slightly modified yet completely untested rule:
rule "R-DeviceID Regex Replacement"
when
has_field("R-DeviceID")
then
let the_result = regex_replace("(\\d+)\\/(\\d+)\\/(\\d+)\\/(\\d+)\\:(\\d+)\\.(\\d+)\\.(\\d+)",to_string($message."R-DeviceID"),"$3-$4-$5-$6-$7",true);
set_field("R-DeviceID", the_result);
end
rule "DHCPv6 R-DeviceID Regex Replacement"
when
has_field("R_DeviceID")
then
let DeviceID = regex_replace("(\\d+)\\/(\\d+)\\/(\\d+)\\/(\\d+)\\:(\\d+)\\.(\\d+)\\.(\\d+)", to_string($message."R_DeviceID"),"$3-$4-$5-$6-$7",true);
set_field ("R_DeviceID", DeviceID);
end
The Goal
Reformating this Value 1/1/01/02:2.1.1 to 01-02-2-1-1 after it has been parsed from message string by using Grok Pattern to many Fields one of them is named “R-DeviceID” and the Value 1/1/01/02:2.1.1 is stored in it.
Final Result
rule "DHCPv6 R-DeviceID Regex Replacement"
when
has_field("R_DeviceID")
then
let DeviceID = regex_replace("(\\d+)\\/(\\d+)\\/(\\d+)\\/(\\d+)\\:(\\d+)\\.(\\d+)\\.(\\d+)", to_string($message."R_DeviceID"),"$3-$4-$5-$6-$7",true);
set_field ("R_DeviceID", DeviceID);
end
Regex Replacement should be placed in Stage 1 and the other Grok Pattern Rules should be in Stage 0. (Stage priority. The lower the number, the earlier it will execute.)