Early Access: The content on this website is provided for informational purposes only in connection with pre-General Availability Qlik Products.
All content is subject to change and is provided without warranty.
Skip to main content Skip to complementary content

ReplaceRegExGroup - script and chart function

ReplaceRegExGroup() returns a string after replacing one or more matches between an input string and the composite regular expression pattern that is specified. The text that replaces the matching text is specified in the to_str argument. The function is non-recursive and works from left to right, but if you specify a negative occurrence value, the functions reads from right to left..

This function performs regex operations that are case-sensitive. You can alternatively use the variant ReplaceRegExGroupI() to perform case-insensitive regex operations.

Syntax:  

ReplaceRegExGroup (text, regex, to_str, group [, occurrence])

Return data type: string

Arguments
Argument Description
text The input string text that you want to replace, fully or partially, with the new text from to_str.
regex The regular expression that defines when to replace text. Matches between this argument and the text argument are replaced.
to_str The new text that you want to use to replace existing content from text.
group

The number of the group, in the case of a composite regular expression.

If the regular expression only contains one group, use the ReplaceRegEx() function instead. Alternatively, use ReplaceRegExGroup() with a group value of 0.

You can specify a negative group value to search for groups from right to left.

occurrence

The number of the match (between input text and regular expression) to replace with new text.

This is an optional argument. The default is 0 if omitted. When a value of 0 is used, or if the argument is omitted, all matches between the text and regex are replaced with to_str.

You can specify a negative value for occurrence if you want to identify matches from right to left.

Function examples
Example Result
ReplaceRegExGroup('abc123','([a-z])([0-9]+)','x',0) Returns abx (replace complete match).
ReplaceRegExGroup('abc123','([a-z])([0-9]+)','x',1) Returns abx123 (only replace first group in match).
ReplaceRegExGroup('abc123','([a-z])([0-9]+)','x',2) Returns abcx (only replace second group in match).
ReplaceRegExGroup('abc123abc123','([a-z])([0-9]+)','x',1,2) Returns abc123abx123. There are two matches between the first group of the input text and the regex pattern. However, only the second occurrence is replaced.
ReplaceRegExGroup('ABC123ABC123','([a-z])([0-9]+)','x',1) Returns ABC123ABC123. In other words, no text is replaced. This is because the function is case-sensitive, and the case-insensitive variant of the function is not used.
ReplaceRegExGroupI('ABC123ABC123','([a-z])([0-9]+)','x',1) Returns ABx123ABx123. In this example, text replacement happens because the case-insensitive variant of the function, ReplaceRegExGroupI(), is used.

When to use it

You can use ReplaceRegExGroup() for modifying text to meet formatting and compliance standards, especially when you need to identify complex patterns in data. The additional group argument for this function allows you to update only certain portions of a larger text pattern. For example, you can update specific parts of a URL.

If needed, you can also use this function to mask sensitive information, such as personal identifiable information (PII), so that it is not shown to users analyzing your app.

Example 1 – load script to update domain in URLs

Example 2 – chart expression to update protocol in URLs

Did this page help you?

If you find any issues with this page or its content – a typo, a missing step, or a technical error – let us know how we can improve!