Regular expression functions extract, match, replace, and split string values using regex patterns in data transformation rules.
Function list
|
Category |
Function |
Description |
|
Value extraction function |
Extracts a value that matches a regular expression. |
|
|
Returns a list of all values that match a regular expression. |
||
|
Matching |
Checks whether a value matches a regular expression. |
|
|
Replacement |
Replaces specified characters in a string based on a regular expression. |
|
|
Splitting |
Splits a string into an array of strings. |
regex_select
Extracts a value that matches a regular expression.
-
Syntax
regex_select(value, r"regular expression", mi=None, gi=None) -
Parameters
Parameter Name
Parameter Type
Required
Description
value
any
Yes
The value to match.
regular expression
String
Yes
The regular expression for matching.
mi
int
No
The index of the match to return. The default value is None or 0, which returns the first match.
gi
int
No
The index of the capturing group to return. The default value is None or 0, which returns the first group.
-
Response
Returns the matched value.
-
Examples
-
Example 1: Extract the first value from the str field that matches the regular expression.
-
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 from the str field that match the regular expression.
-
Raw log
str: abc123 xyz456 -
Transformation rule
# Extract the first value from the str field that matches the regular expression. e_set("regex", regex_select(v("str"), r"\d+")) # Extract the second value from the str field that matches the regular expression. 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 value of the first group from the first expression in the str field that matches the regular expression. e_set("regex", regex_select(v("str"),r"[a-z]+(\d+)",gi=0)) # Extract the value of the first group from the second expression in the str field that matches the regular expression. e_set("regex2", regex_select(v("str"),r"[a-z]+(\d+)",mi=1,gi=0)) # Extract the value of the first group from the first expression in the str field that matches the regular expression. e_set("regex3", regex_select(v("str"),r"([a-z]+)(\d+)",gi=0)) # Extract the value of the second group from the first expression in the str field that matches the regular expression. 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
Returns an array of all values that match a regular expression.
-
Syntax
regex_findall(value, r"regular expression") -
Parameters
Parameter Name
Parameter Type
Required
Description
value
any
Yes
The value to match.
regular expression
String
Yes
The regular expression.
-
Response
Returns a list of all matching values.
-
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
Checks whether a value matches a regular expression.
-
Syntax
regex_match(value, r"regular expression", full=False) -
Parameters
Parameter Name
Parameter type
Required
Description
value
any
Yes
The value to match.
regular expression
string
Yes
The regular expression.
full
Bool
No
Specifies whether to require a full match. The default value is False.
-
Response
Returns true or false indicating whether the value matches.
-
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 consists of 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
Replaces specified characters in a string based on a regular expression.
-
Syntax
regex_replace(value, r"regular expression", replace="", count=0) -
Parameters
Parameter name
Parameter Type
Required
Description
value
any
Yes
The value to be replaced.
regular expression
string
Yes
The regular expression.
replace
string
No
The replacement string. The default value is an empty string, which deletes the matched characters.
Regular expressions are supported. For example,
r"\1****\2"specifies that the replacement string uses capturing group references.-
\1represents the first group. -
\2represents the second group.
count
Number
No
The number of replacements. The default value is 0, which replaces all matches.
-
-
Response
The string after replacement.
-
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 the middle four digits 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 replacement string is formatted asr"\1****\2". -
\1represents the first group, which is(\d{0,3}). -
\2represents the second group, which is(\d{4}).
-
-
Result
iphone: 13900001234 sec_iphone: 139****1234
-
-
regex_split
Splits a string into an array of strings.
-
Syntax
regex_split(value, r"regular expression", maxsplit=0) -
Parameters
Parameter Name
Parameter type
Required
Description
value
any
Yes
The value to split.
regular expression
string
Yes
The regular expression.
maxsplit
int
No
The maximum number of splits. The default value is 0, which splits the string at all matches. A value of 1 splits only at the first match.
-
Response
An array of the resulting substrings.
-
Examples
Split the str field by numbers:
-
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"]
-