This topic describes the syntax of regular expression functions and provides parameter description and function examples.

Functions

Type Function Description
Value extraction regex_select Extracts a specific value based on a regular expression.
regex_findall Obtains all values that match a regular expression.
Match judgment regex_match Checks whether a value matches a regular expression.
Value replacement regex_replace Replaces specified characters in a string based on a regular expression.
String splitting regex_split Splits a value to multiple values.

regex_select

  • Syntax
    regex_select(Value, r"Regular expression", mi=None, gi=None)
  • Parameters
    Parameter Type Required? Description
    Value Any type Yes The value to match with the regular expression.
    Regular expression String Yes The regular expression to match.
    mi Int No The sequence number of the match to return. Default value: None, indicating that the first match is returned.
    gi Int No The sequence number of the group to match. Default value: None, indicating that the first group is matched.
  • Response

    The extracted value is returned.

  • Examples
    • Example 1: Extract a number from a string.
      Raw log:
      str: iZbp1a65x3r1vhpe94fi2qZ
      Processing rule:
      e_set("regex", regex_select(v("str"),r"\d+"))
      Processing result:
      str: iZbp1a65x3r1vhpe94fi2qZ
      regex:  1
    • Example 2
      Raw log:
      str: abc123 xyz456
      Processing rule:
      e_set("regex", regex_select(v("str"),r"\d+",mi=1))
      e_set("regex2", regex_select(v("str"),r"\d+"))
      Processing result:
      str: abc123 xyz456
      regex:   456
      regex2:  123
    • Example 3
      Raw log:
      str: abc123 xyz456
      Processing rule:
      e_set("regex", regex_select(v("str"),r"[a-z]+(\d+)",gi=0))
      e_set("regex2", regex_select(v("str"),r"[a-z]+(\d+)",mi=1,gi=0))
      e_set("regex3", regex_select(v("str"),r"([a-z]+)(\d+)",gi=0))
      e_set("regex4", regex_select(v("str"),r"([a-z]+)(\d+)",gi=1))
      Processing result:
      str: abc123 xyz456
      regex:   123
      regex2:  456
      regex3:  abc
      regex4:  123

regex_findall

  • Syntax
    regex_findall(Value, r"Regular expression")
  • Parameters
    Parameter Type Required? Description
    Value Any type Yes The value to match with the regular expression.
    Regular expression String Yes The regular expression to match.
  • Response

    The values that match the regular expression are returned.

  • Example: Find all numbers in a string.
    Raw log:
    str: iZbp1a65x3r1vhpe94fi2qZ
    Processing rule:
    e_set("regex_findall", regex_findall(v("str"),r"\d+"))
    Processing result:
    str: iZbp1a65x3r1vhpe94fi2qZ
    regex_findall:  ["1", "65", "3", "1", "94", "2"]

regex_match

  • Syntax
    regex_match( Value, r"Regular expression", full=False)
  • Parameters
    Parameter Type Required? Description
    Value Any type Yes The value to match with the regular expression.
    Regular expression String Yes The regular expression to match.
    full Boolean No Specifies whether the value needs to fully match the regular expression. Default value: False.
  • Response

    A value of True or False is returned.

  • Example: Find all numbers in a string.
    Raw log:
    str: iZbp1a65x3r1vhpe94fi2qZ
    Processing rule:
    e_set("regex_match", regex_match(v("str"),r"\d+"))
    e_set("regex_match2", regex_match(v("str"),r"\d+",full=True))
    Processing result:
    str: iZbp1a65x3r1vhpe94fi2qZ
    regex_match:  True
    regex_match2:  False

regex_replace

  • Syntax
    regex_replace(Value, r"Regular expression", replace="", count=0)
  • Parameters
    Parameter Type Required? Description
    Value Any type Yes The value for which you want to replace the specified characters.
    Regular expression String Yes The regular expression that specifies the characters to replace.
    replace String No The characters used to replace the specified characters in the value. The default value is an empty string, indicating that the specified characters are deleted.
    count Number No The number of matches to replace. Default value: 0, indicating that all matches are replaced.
  • Response

    The updated value is returned.

  • Example: Replace all numbers in a string with 13.
    Raw log:
    str: iZbp1a65x3r1vhpe94fi2qZ
    replace: 13
    Processing rule:
    e_set("regex_replace", regex_replace(v("str"),r"\d+",v("replace")))
    Processing result:
    str: iZbp1a65x3r1vhpe94fi2qZ
    replace: 13
    regex_replace:  iZbp13a13x13r13vhpe13fi13qZ

regex_split

  • Syntax
    regex_split(Value, r"Regular expression", maxsplit=0)
  • Parameters
    Parameter Type Required? Description
    Value Any type Yes The value to split.
    Regular expression String Yes The regular expression to match.
    maxsplit Int No The maximum number of splits. Default value: 0, specifying that the value is split based on all matches. A value of 1 specifies that the value is split when the first match is found and the remaining part of the value is not split.
  • Response

    The split values are returned.

  • Example: Split a string based on numbers.
    Raw log:
    str: iZbp1a65x3r1vhpe94fi2qZ
    Processing rule:
    e_set("regex_split", regex_split(v("str"),r"\d+"))
    Processing result:
    str: iZbp1a65x3r1vhpe94fi2qZ
    regex_split:  ["iZbp", "a", "x", "r", "vhpe", "fi", "qZ"]