Home > Net >  Telegraf Regex expression to fetch last string
Telegraf Regex expression to fetch last string

Time:09-24

As per my requirement I want to fetch last few characters in a string. For example my string is "hello/how/are/you", here I want only "are/you".

[[processors.regex]]
  order = 1305
  namepass = ["*_promitor_test"]
  # Tag and field conversions defined in a separate sub-tables
  [[processors.regex.tags]]
    ## Tag to change
    key = "resource"
    ## Regular expression to match on a tag value
    pattern = "^(.*)$/are.*"
    ## Matches of the pattern will be replaced with this string.
    replacement = "${0}"
    result_key = "resource_type"

But I am not getting expected output i.e. "are/you". Can anybody please help me with this expression?

CodePudding user response:

I don't know that language but for the regex

   #edited the regex
   pattern = "^(.*)(are.*)$"
   #group number starts from 1 not 0 
   replacement = "${2}"

CodePudding user response:

Use

.*(are.*)

In your snippet:

[[processors.regex]]
  order = 1305
  namepass = ["*_promitor_test"]
  # Tag and field conversions defined in a separate sub-tables
  [[processors.regex.tags]]
    ## Tag to change
    key = "resource"
    ## Regular expression to match on a tag value
    pattern = ".*(are.*)"
    ## Matches of the pattern will be replaced with this string.
    replacement = "$1"
    result_key = "resource_type"

See regex proof.

EXPLANATION

NODE                     EXPLANATION
--------------------------------------------------------------------------------
  .*                       any character except \n (0 or more times
                           (matching the most amount possible))
--------------------------------------------------------------------------------
  (                        group and capture to \1:
--------------------------------------------------------------------------------
    are                      'are'
--------------------------------------------------------------------------------
    .*                       any character except \n (0 or more times
                             (matching the most amount possible))
--------------------------------------------------------------------------------
  )                        end of \1
  • Related