github.com/GuanceCloud/cliutils@v1.1.21/pipeline/ptinput/funcs/md/add_pattern.en.md (about)

     1  ### `add_pattern()` {#fn-add-pattern}
     2  
     3  Function prototype: `fn add_pattern(name: str, pattern: str)`
     4  
     5  Function description: Create custom grok patterns. The grok pattern has scope restrictions, such as a new scope will be generated in the if else statement, and the pattern is only valid within this scope. This function cannot overwrite existing grok patterns in the same scope or in the previous scope
     6  
     7  Function parameters:
     8  
     9  - `name`: pattern naming
    10  - `pattern`: custom pattern content
    11  
    12  Example:
    13  
    14  ```python
    15  # input data: "11,abc,end1", "22,abc,end1", "33,abc,end3"
    16  
    17  # script
    18  add_pattern("aa", "\\d{2}")
    19  grok(_, "%{aa:aa}")
    20  if false {
    21  
    22  } else {
    23      add_pattern("bb", "[a-z]{3}")
    24      if aa == "11" {
    25          add_pattern("cc", "end1")
    26          grok(_, "%{aa:aa},%{bb:bb},%{cc:cc}")
    27      } elif aa == "22" {
    28          # Using pattern cc here will cause compilation failure: no pattern found for %{cc}
    29          grok(_, "%{aa:aa},%{bb:bb},%{INT:cc}")
    30      } elif aa == "33" {
    31          add_pattern("bb", "[\\d]{5}") # Overwriting bb here fails
    32          add_pattern("cc", "end3")
    33          grok(_, "%{aa:aa},%{bb:bb},%{cc:cc}")
    34      }
    35  }
    36  
    37  # result
    38  {
    39      "aa":      "11"
    40      "bb":      "abc"
    41      "cc":      "end1"
    42      "message": "11,abc,end1"
    43  }
    44  {
    45      "aa":      "22"
    46      "message": "22,abc,end1"
    47  }
    48  {
    49      "aa":      "33"
    50      "bb":      "abc"
    51      "cc":      "end3"
    52      "message": "33,abc,end3"
    53  }
    54  ```