Logstash if语句和正则表达式示例 [英] Logstash if statement with regex example

查看:1713
本文介绍了Logstash if语句和正则表达式示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以告诉我在logstash中带有正则表达式的if语句是什么样的吗?

Can anyone show me what an if statement with a regex looks like in logstash?

我的尝试:

if [fieldname] =~ /^[0-9]*$/

if [fieldname] =~ "^[0-9]*$"

两者都不起作用.

我打算做的是检查字段名"是否包含整数

What I intend to do is to check if the "fieldname" contains an integer

推荐答案

将其他答案组合成一个有凝聚力的答案.

To combine the other answers into a cohesive answer.

您的第一种格式看起来正确,但是您的正则表达式未满足您的要求.

Your first format looks correct, but your regex is not doing what you want.

/^[0-9]*$/匹配:

^:该行的开头

[0-9]*:0次或多次的任意数字

[0-9]*: any digit 0 or more times

$:该行的结尾

因此您的正则表达式捕获仅由数字组成的行.要在只包含一个或多个数字的字段上进行匹配,请尝试使用等价的/[0-9]+//\d+/,并且无论该行的其余部分如何,每个匹配1个或多个数字.

So your regex captures lines that are exclusively made up of digits. To match on the field simply containing one or more digits somewhere try using /[0-9]+/ or /\d+/ which are equivalent and each match 1 or more digits regardless of the rest of the line.

您总共应该拥有:

if [fieldname] =~ /\d+/ {
   # do stuff
}

这篇关于Logstash if语句和正则表达式示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆