Skip to main content
Version: Next

Match regex

Match regex ( pattern ; aString ; start {; pos_found ; length_found}{; *} ) -> Function result 

        Match regex ( *pattern* ; *aString* ) -> Function result
ParameterTypeDescription
patternText🡒Regular expression
aStringText🡒String in which search will be done
startLongint🡒Position in aString where search will start
pos_foundLongint array, Longint variable🡘Position of occurrence
length_foundLongint array, Longint variable🡘Length of occurrence
*Operator🡒If passed: only searches at position indicated
Function resultBoolean🡐True = search has found an occurrence; Otherwise, False.
Match regex ( pattern ; aString ) -> Function result
ParameterTypeDescription
patternText🡒Regular expression (complete equality)
aStringText🡒String in which search will be done
Function resultBoolean🡐True = search has found an occurrence; Otherwise, False.

Description

The Match regex command checks the conformity of a character string with respect to a set of synthesized rules by means of a meta-language called “regular expression” or “rational expression.” The regex abbreviation is commonly used to indicate these types of notations.

Pass the regular expression to search for in pattern. This consists of a set of characters used for describing a character string, using special characters.

Pass the string where you want to search for the regular expression in aString.

In start, pass the position at which to start the search in aString.

If pos_found and length_found are variables, the command returns the position and length of the occurrence in these variables. If you pass arrays, the command returns the position and length of the occurrence in the element zero of the arrays and the positions and lengths of the groups captured by the regular expression in the following elements.

The optional * parameter indicates, when it is passed, that the search must be carried out at the position specified by start without searching any further in the case of failure.

The command returns True if the search has found an occurrence.

For more information about regex, refer to the following address:
http://en.wikipedia.org/wiki/Regular%5Fexpression

For more information about the syntax of the regular expression passed in the pattern parameter, refer to the following address:
https://unicode-org.github.io/icu/userguide/strings/regexp.html#regular-expressions

Example 1

Search for complete equality (simple syntax):
vfound:=Match regex(pattern;mytext)

 QUERY BY FORMULA([Employees];Match regex(".*smith.*";[Employees]name))

Example 2

Search in text by position:
vfound:=Match regex( pattern;mytext; start; pos_found; length_found)
Example to display all the $1 tags:

 $start:=1
 Repeat
    vfound:=Match regex("<.*>";$1;$start;pos_found;length_found)
    If(vfound)
       ALERT(Substring($1;pos_found;length_found))
       $start:=pos_found+length_found
    End if
 Until(Not(vfound))

Example 3

Search with support of “capture groups” via parentheses. ( ) are used to specify groups in the regexes:
vfound:=Match regex( pattern;mytext; start; pos_found_array; length_found_array)

 ARRAY LONGINT(pos_found_array;0)
 ARRAY LONGINT(length_found_array;0)
 vfound:=Match regex("(.*)stuff(.*)";$1;1;pos_found_array;length_found_array)
 If(vfound)
    $group1:=Substring($1;pos_found_array{1};length_found_array{1})
    $group2:=Substring($1;pos_found_array{2};length_found_array{2})
 End if

Example 4

Search limiting the comparison of the pattern to the position indicated:
Add a star to the end of one of the two previous syntaxes.

 vfound:=Match regex("a.b";"---a-b---";1;$pos_found;$length_found)
  //returns True
 vfound:=Match regex("a.b";"---a-b---";1;$pos_found;$length_found;*)
  //returns False
 vfound:=Match regex("a.b";"---a-b---";4;$pos_found;$length_found;*)
  //returns True

Note: The positions and lengths returned are only meaningful in Unicode mode or if the text being worked with is of the 7-bit ASCII type.

Error management

In the event of an error, the command generates an error that you can intercept via a method installed by the ON ERR CALL command.