This topic describes the syntax and parameters of regular expression functions. This topic also provides examples on how to use the functions.

Functions

Category Function Description
Value extraction regex_select Extracts a value that matches a regular expression.
regex_findall Extracts the list of all values that match a regular expression.
Evaluation regex_match Checks whether a value matches a regular expression.
Replacement regex_replace Replaces the characters that match a regular expression in a string.
Splitting regex_split Splits a string into an array of strings.

regex_select

The regex_select function extracts a value that matches a regular expression.
  • Syntax

    regex_select(value, r"regular expression", mi=None, gi=None)
  • Parameters

    Parameter Type Required Description
    value Arbitrary Yes The input value.
    regular expression String Yes The regular expression.
    mi int No The sequence number of the value that is matched in the input value and is returned. Default values: None and 0. The default values indicate that the first value that is matched is returned.
    gi int No The sequence number of the group that is used for matching in the regular expression. Default values: None and 0. The default values indicate that the first group is used.
  • Response

    The extracted value is returned.

  • Examples

    • Example 1: Extract the first value that matches the regular expression from the str field.
      • Raw log
        str: iZbp1a65x3r1vhpe94fi2qZ
      • Transformation rule
        e_set("regex", regex_select(v("str"), r"\d+"))
        e_set("regex2", regex_select(v("str"), r"\d+", mi=None))
        e_set("regex3", regex_select(v("str"), r"\d+", mi=0))
      • Result
        regex:1
        regex2:1
        regex3:1
        str:iZbp1a65x3r1vhpe94fi2qZ
    • Example 2: Extract the first and second values that match the regular expression from the str field.
      • Raw log
        str: abc123 xyz456
      • Transformation rule
        # Extract the first value that matches the regular expression from the str field. 
        e_set("regex", regex_select(v("str"), r"\d+"))
        # Extract the second value that matches the regular expression from the str field. 
        e_set("regex2", regex_select(v("str"), r"\d+", mi=1))
      • Result
        regex:   123
        regex2:  456
        str: abc123 xyz456
    • Example 3
      • Raw log
        str: abc123 xyz456
      • Transformation rule
        # Extract the first value that matches the first group in the regular expression from the str field. 
        e_set("regex", regex_select(v("str"),r"[a-z]+(\d+)",gi=0))
        # Extract the second value that matches the first group in the regular expression from the str field. 
        e_set("regex2", regex_select(v("str"),r"[a-z]+(\d+)",mi=1,gi=0))
        # Extract the first value that matches the first group in the regular expression from the str field. 
        e_set("regex3", regex_select(v("str"),r"([a-z]+)(\d+)",gi=0))
        # Extract the first value that matches the second group in the regular expression from the str field. 
        e_set("regex4", regex_select(v("str"),r"([a-z]+)(\d+)",gi=1))
      • Result
        str: abc123 xyz456
        regex:   123
        regex2:  456
        regex3:  abc
        regex4:  123

regex_findall

The regex_findall function extracts the list of all values that match a regular expression.
  • Syntax

    regex_findall(value, r"regular expression")
  • Parameters

    Parameter Type Required Description
    value Arbitrary Yes The input value.
    regular expression String Yes The regular expression.
  • Response

    The list of all values that match the regular expression is returned.

  • Examples

    Extract all numbers from the str field.
    • Raw log
      str: iZbp1a65x3r1vhpe94fi2qZ
    • Transformation rule
      e_set("regex_findall", regex_findall(v("str"),r"\d+"))
    • Result
      str: iZbp1a65x3r1vhpe94fi2qZ
      regex_findall:  ["1", "65", "3", "1", "94", "2"]

regex_match

The regex_match function checks whether a value matches a regular expression.
  • Syntax

    regex_match(value, r"regular expression", full=False)
  • Parameters

    Parameter Type Required Description
    value Arbitrary Yes The input value.
    regular expression String Yes The regular expression.
    full Bool No Specifies whether to perform full match. Default value: False.
  • Response

    The value True or the value False is returned.

  • Examples

    Check whether the str field contains numbers.
    • Raw log
      str: iZbp1a65x3r1vhpe94fi2qZ
    • Transformation rule
      # Check whether the str field contains numbers. 
      e_set("regex_match", regex_match(v("str"),r"\d+"))
      # Check whether the str field contains only numbers. 
      e_set("regex_match2", regex_match(v("str"),r"\d+",full=True))
    • Result
      str: iZbp1a65x3r1vhpe94fi2qZ
      regex_match:  True
      regex_match2:  False

regex_replace

The regex_replace function replaces the characters that match a regular expression in a string.
  • Syntax

    regex_replace(value, r"regular expression", replace="", count=0)
  • Parameters

    Parameter Type Required Description
    value Arbitrary Yes The input value whose characters you want to replace.
    regular expression String Yes The regular expression.
    replace String No The characters that you want to use to replace the matched characters in the input value. This parameter is empty by default, which indicates that the matched characters are deleted.
    You can specify a regular expression. Example: r"\1****\2". This value indicates that the output value must match the regular expression.
    • \1 indicates the first group.
    • \2 indicates the second group.
    count Number No The number of times that you want to replace the matched characters. Default value: 0. The default value indicates that all matched characters are replaced.
  • Response

    The value after replacement is returned.

  • Examples

    • Example 1: Replace all numbers in the str field with 13.
      • Raw log
        str: iZbp1a65x3r1vhpe94fi2qZ
        replace: 13
      • Transformation rule
        e_set("regex_replace", regex_replace(v("str"),r"\d+",v("replace")))
      • Result
        str: iZbp1a65x3r1vhpe94fi2qZ
        replace: 13
        regex_replace:  iZbp13a13x13r13vhpe13fi13qZ
    • Example 2: Mask four digits in the middle of a mobile phone number.
      • Raw log
        iphone: 13900001234
      • Transformation rule
        e_set(
            "sec_iphone",
            regex_replace(v("iphone"), r"(\d{0,3})\d{4}(\d{4})", replace=r"\1****\2"),
        )
        Note
        • replace=r"\1****\2" indicates that the value after replacement must match the regular expression r"\1****\2".
        • \1 indicates the first group. In this example, (\d{0,3}) is the first group.
        • \2 indicates the second group. In this example, (\d{4}) is the second group.
      • Result
        iphone: 13900001234
        sec_iphone: 139****1234

regex_split

The regex_split function splits a string into an array of strings.
  • Syntax

    regex_split(value, r"regular expression", maxsplit=0)
  • Parameters

    Parameter Type Required Description
    value Arbitrary Yes The input value that you want to split.
    regular expression String Yes The regular expression.
    maxsplit int No The maximum number of times that the input value can be split. Default value: 0. The default value indicates that the input value is split based on all matched characters. The value 1 indicates that the input value is split based only on the first matched character.
  • Response

    An array that contains the values after splitting is returned.

  • Examples

    Example: Split the str field by number.
    • Raw log
      str: iZbp1a65x3r1vhpe94fi2qZ
    • Transformation rule
      e_set("regex_split", regex_split(v("str"),r"\d+"))
    • Result
      str: iZbp1a65x3r1vhpe94fi2qZ
      regex_split:  ["iZbp", "a", "x", "r", "vhpe", "fi", "qZ"]