Logstash if statement with regular expression example

Can someone show me what if expression with regex looks in logstash?

My attempts:

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

None of them work.

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

+6
source share
4 answers

Combine other answers into a single answer.

Your first format looks right, but your regular expression does not do what you want.

/^[0-9]*$/ matches:

^ : start of line

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

$ : end of line

So, your regex captures strings that consist solely of numbers. To match fields containing only one or more digits, try using /[0-9]+/ or /\d+/ , which are equivalent, and each of them corresponds to 1 or more digits regardless of the rest of the line.

In total you must have:

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

^ states the position at the beginning of a line

$ asserts a position at the end of a line

Your regex just matches the number string, and the check contains an integer that needs ^ and $ to be removed.

+3
source

The easiest way is to check \d

 if [fieldname] =~ /\d+/ { ... } 
+3
source

You need this regex (and brackets, I think):

 if ([fieldname] =~ /^[0-9]+$/) 
0
source

Source: https://habr.com/ru/post/1015117/


All Articles