0

根据我的要求,我想获取字符串中的最后几个字符。例如我的字符串是“hello/how/are/you”,这里我只想要“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"

但我没有得到预期的输出,即“是/你”。有人可以帮我解决这个问题吗?

4

2 回答 2

2

我不知道那种语言,但对于正则表达式

   #edited the regex
   pattern = "^(.*)(are.*)$"
   #group number starts from 1 not 0 
   replacement = "${2}"
于 2021-09-23T17:50:16.297 回答
0

利用

.*(are.*)

在您的代码段中:

[[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"

请参阅正则表达式证明

解释

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
于 2021-09-23T21:51:50.497 回答