SUBSTRINGCOUNT Function

Returns the number of sub-patterns found to have matched the pattern specified by the compiled regular expression.

Category: Regular Expression
Requirement: The regular expression must contain sub-patterns that can be used to match patterns.
Returned data type: Integer

Syntax

r.SUBSTRINGCOUNT( )

Arguments

There is no argument for this function.

Details

Use the SUBSTRINGCOUNT function to find the total number of sub-patterns found to have matched the regular expression. Normally simple regular expressions evaluate to "1", but if you design regular expressions using sub-patterns then this function will return the number found.

The function returns a positive integer that specifies the number of substrings found to have matched the regular expression. A "0" is returned if no substrings are found.

The syntax for using subpatterns is open and closed parentheses. For example:

(Mr|Mrs) Smith

For this example, the sub-pattern is the "(Mr|Mrs)". Using this function returns the number "2" for the count of substrings since the entire string is considered the first sub-pattern. The part inside the parentheses is the second sub-pattern.

This function can provide the upper number for a logical loop using the FOR command so that your code can iterate through the matched sub-patterns for comparison to other values.

Example

Note: This example can be run in a stand-alone expression node if the Generate rows when no parent is specified option is selected. If passing data to this node, turn this setting off and remove the SETEOF() function. The PUSHROW statements are also unnecessary if passing data values in to the node as data rows. Unless stated otherwise, all code shown should be entered in the Expression tab of the Expression node.

//Define some variables

string MyString

string MyString2

integer i

integer SSC

integer SSS

integer SSL

 

// Set initial values for variables

i = 0

SSS = 0

SSL = 0

SSC = 0

 

// Sample inpit string

MyString = "DataFlux Data Management Studio"

 

// Define a regular expression object

regex r

// Then compile it - notice the use of ( and )

r. compile("(DataFlux|DF) Data Management (Studio|Platform)")

// Find the first substring

if r.findfirst(MyString)

   begin

      // Use the "substring" functions to find the number of substrings

      SSC = r.substringcount()

      // Loop through substrings

      for i = 1 to SSC

      begin

      // Then pull out substrings

      SSS = r.substringstart(i)

      SSL = r.substringlength(i)

      MyString2 = mid(MyString,SSS,SSL)

      // Place the substrings in a data row

      pushrow()

      end

   end

// Terminate the expression node processing

seteof(true)

// Prevent the last pushrow() from showing up twice

return false


Related Topics