ExtractRegEx - script and chart function
ExtractRegEx() extracts text from an input string expression using the specified regular expression pattern. The function returns a null value if no matches are found.
This function performs regex operations that are case-sensitive. You can alternatively use the variant ExtractRegExI() to perform case-insensitive regex operations.
Syntax:
ExtractRegEx (text, regex [, field_no])
Return data type: string
Argument | Description |
---|---|
text | String expression containing the text to be extracted in the return value. |
regex | The regular expression to use for extracting text. |
field_no |
The number of the match to extract. This is useful when multiple matches for the regular expression might be found in the text. For example, specify a value of 4 to extract the fourth match. This is an optional argument. Specify a negative value to reverse the order of the matches. The following apply regardless of whether the function is used in a load script or chart expression:
When using the function in a load script:
When using the function in a chart expression:
|
Example | Result |
---|---|
ExtractRegEx('a,b c;1 2,3','[a-z]|[0-9]', 1) | Returns a, which is the first match found. |
ExtractRegEx('a,b c;1 2,3','[a-z]|[0-9]', 2) | Returns b, which is the second match found. |
ExtractRegEx('a,b c;1 2,3','[a-z]|[0-9]', 4) | Returns 1, which is the fourth match found. |
ExtractRegEx('abc','a|b',-1) | Returns b. When a negative value is specified, matches are counted in reverse. |
ExtractRegEx('A,B C;1 2,3','[a-c]|[4-9]') | Returns a null value, because ExtractRegEx() is case-sensitive, and the case-insensitive variant was not used. |
ExtractRegExI('A,B C;1 2,3','[a-c]|[4-9]') | Returns A, because the case-insensitive variant ExtractRegExI() was used. |
When to use it
You can use Extract RegEx() to extract information that you want to isolate from data that might also contain other information (for example, free text or JSON strings). For example:
-
Extract email addresses, telephone numbers, account numbers, and other information from text.
-
Extract numeric values from text (for example, currency).
-
Standardize formatting of text or numeric data.