简体   繁体   English

如何使用gsub将包含下划线的部分字符串转换为logstash中的括号

[英]How to convert part of a string that includes underscores to brackets in logstash with gsub

I want to convert, for eg Hello_1_.Bye to Hello[1].Bye Note that [1], ie, within brackets contain only digits例如,我想将 Hello_1_.Bye 转换为 Hello[1].Bye 请注意 [1],即括号内仅包含数字

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感谢你的帮助

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.这是一个正则表达式演示,显示替换正在工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM