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.
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.
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.
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.
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
Overview
Open the Data load editor and add the load script below to a new tab.
The load script contains:
A table URLs that initially contains a list of input strings in which we want to identify and update specific URLs. We want to update the domain in these URLs.
The creation of a field, ResourceURL_Updated, that contains modified input text with the processed URLs.
A variableURL_RegEx to store the regular expression being used to identify URLs to process.
Our requirements:
Each input string contains two URLs, with the resource URL specified in the first URL. We do not want to update any URLs other than the first one listed.
The URL to update needs to be in the format <https or https>://<domain>.com/<resource path>. The resource path is optional but is not used in this example.
Load script
Set URL_RegEx = '(https?):\/\/(([a-zA-Z0-9]+)\.([a-zA-Z0-9]*)\.??([a-zA-Z0-9]*))\/?((([a-zA-Z0-9]*)*)((\/?([a-zA-Z0-9]*)*))*)\/{0,1}([\.,;]+(?=\s))*?';
URLs:
Load
ReplaceRegExGroup(URLsList, '$(URL_RegEx)', 'replacement-server', 2,1) as ResourceURL_Updated;
Load * Inline `
URLsList
The resource is located at https://testserver.com/files. For more help, see: https://support.company.com.
The resource is located at https://testserver.com/files/worksheet. For more help, see: https://support.company.com.
The resource is located at https://testserver.com/files/book. For more help, see: https://support.company.com.
The resource is located at https://testserver.com/files/form. For more help, see: https://support.company.com.
The resource is located at https://testserver.com/datamodel. For more help, see: https://support.company.com.
The resource is located at https://testserver.com/resourcenew. For more help, see: https://support.company.com.
The resource is located at https://testserver.com/page. For more help, see: https://support.company.com.
The resource is located at https://testserver.com/page/overview. For more help, see: https://support.company.com.
` (delimiter is '\t');
Results
Load the data and open a sheet. Create a new table and add this field as a dimension:
ResourceURL_Updated
Results table
ResourceURL_Updated
The resource is located at https://replacement-server/datamodel. For more help, see: https://support.company.com.
The resource is located at https://replacement-server/files. For more help, see: https://support.company.com.
The resource is located at https://replacement-server/files/book. For more help, see: https://support.company.com.
The resource is located at https://replacement-server/files/form. For more help, see: https://support.company.com.
The resource is located at https://replacement-server/files/worksheet. For more help, see: https://support.company.com.
The resource is located at https://replacement-server/page. For more help, see: https://support.company.com.
The resource is located at https://replacement-server/page/overview. For more help, see: https://support.company.com.
The resource is located at https://replacement-server/resourcenew. For more help, see: https://support.company.com.
Example 2 – chart expression to update protocol in URLs
Overview
Open the Data load editor and add the load script below to a new tab.
The load script contains:
A table URLsList containing a list of input strings in which we want to identify and update specific URLs. We want to update the protocol from http to https in the links to the Support website.
Our requirements:
Each input string contains two URLs, with the Support website link appearing as the second URL. We do not want to update any URLs other than the second URL.
The URL to update needs to be in the format <https or https>://<domain>.com/<resource path>. The resource path is optional but is not used in this example.
Load script
URLs:
Load * Inline `
URLsList
The resource is located at https://testserver.com/files. For more help, see: http://support.company.com.
The resource is located at https://testserver.com/files/worksheet. For more help, see: http://support.company.com.
The resource is located at https://testserver.com/files/book. For more help, see: http://support.company.com.
The resource is located at https://testserver.com/files/form. For more help, see: http://support.company.com.
The resource is located at https://testserver.com/datamodel. For more help, see: http://support.company.com.
The resource is located at https://testserver.com/resourcenew. For more help, see: http://support.company.com.
The resource is located at https://testserver.com/page. For more help, see: http://support.company.com.
The resource is located at https://testserver.com/page/overview. For more help, see: http://support.company.com.
` (delimiter is '\t');
Results
Load the data and open a sheet. Create a new table and add this field as a dimension: