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

IndexRegEx - script and chart function

IndexRegEx() searches the input string and returns the starting position of the nth occurrence of the specified regular expression pattern. An optional third argument count provides the value of n, which is 1 if omitted. The positions in the string are numbered left to right from 1 and up. If no match is found, the function returns 0.

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

Syntax:  

IndexRegEx (text, regex [, count])

Return data type: integer

Arguments
Argument Description
text The input string text within which you want to search for a regular expression.
regex The regular expression to use for searching the input string.
count

The number of the match. 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 position of the fourth match.

This is an optional argument. Default is 1 if not specified. You can specify a negative value to search for matches from right to left.

Function examples
Example Result
IndexRegEx('abc123','[a-z][0-9]+') Returns 3 (the start position of first match).
IndexRegEx('abc123','[a-z][0-9]+',2) Returns 0 (regex does not have a second match).
IndexRegEx('ABC123','[a-z][0-9]+') Returns 0, because IndexRegEx() is case-sensitive.
IndexRegExI('ABC123','[a-z][0-9]+') Returns 3. The case-insensitive variant of the function, IndexRegExI(), is used.

When to use it

Use cases for this function include:

  • Identifying where specific text patterns occur within larger bodies of text. For example, you might want to know where an email address pattern is used across a series of long email messages.

  • IndexRegEx() is particularly useful for advanced data processing, and is commonly used in the first step of a longer, more complex transformation. It is typically easier to solve problems with other regex functions such as ExtractRegEx(), MatchRegEx(), and CountRegEx(), but there may be times when IndexRegEx() offers solutions that these functions cannot provide.

Example 1 – load script to identify books by ISBN

Example 2 – chart expression to identify contacts with secondary phone numbers

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!