How to convert part of a string that includes underscores to brackets in logstash with gsub
Welcome to Programming Tutorial official website. Today - we are going to cover how to solve / find the solution of this error How to convert part of a string that includes underscores to brackets in logstash with gsub on this date .
I want to convert, for e.g. Hello_1_.Bye to Hello[1].Bye Note that [1], i.e., within brackets contain only digits
I started with something like this that didn’t work..
filter { mutate { gsub => ["String", "*_D_.*", "*[D].*"] //Note that String here could be Hello_1_.Bye, Hello_2_.Bye etc. } }
but getting this error
:exception=>#<RegexpError: target of repeat operator is not specified: /*_D_*/>
Appreciate your help
Answer
I suggest using this version:
filter { mutate { gsub => ["Hello_1_.Bye", "([^_]+)_(d+)_(.w+)", "1[2]3"] } }
Here is a regex demo showing that the replacement is working.