regexp

Regular expression processor.

Available in:

Apps (win) Apps (char) Reportwriter RPC Standalone PL
X X X X X

Syntax

int regexp(option [| flags][,expr1[,expr2]])
int        option
string     expr1
string     expr2

Description

Applies a regular expression against a string and returns true if there is a match or false if there is no match. The options and flags are defined in trim.h and are:
regexp_end (option) returns the ending offset of the match after regexp_exists returns true. Requires the regexp_offsets flag.
regexp_exist (option) executes the cached regular expression tree against expr1 and returns true if there is a match, otherwise false.

If expr2 is supplied, then the regexp processing uses it as the regular expression instead of the cached one. The cached regular expression is not affected.

regexp_free (option) frees the cached regular expresssion.
regexp_init (option) initializes and caches the regular expresssion expr1. Any existing regular expression is automatically replaced.
regexp_start (option) returns the starting offset of the match after regexp_exists returns true. Requires the regexp_offsets flag.
regexp_icase (flag) ignore case when checking for matches.
regexp_newline (flag) "match any" in expr1 does not match newline.
regexp_offsets (flag) calculate start/end offsets.

Notes

If the same regular expression is to be used multiple times, then it is much more efficient to use regexp_init, regexp_exist, regexp_free than regexp_exist with the regular expression.

Regexp_start and regexp_end are only valid when regexp_exists is used without the regular expression argument.

Example

The following demonstrates the performance difference between cached and uncached regular expressions.
#define iters 100000

{
int  i;
char exp[20] = "N*s";
char str[20] = "Niklas Back";

timestamp();

regexp(regexp_init,exp);

for (i=iters;i;i--) regexp(regexp_exist,str);

printf(regexp(regexp_exist,str) ^^ ", Seconds: " ^^ timestamp());

for (i=iters;i;i--) regexp(regexp_exist,str,exp);

printf(regexp(regexp_exist,str,exp) ^^ ", Seconds: " ^^ timestamp());
}   
The following finds the beginning and ending offsets in a string.
{
char exp[20] = "N.*?s";
char str[20] = "Back, Niklas";

regexp(regexp_init|regexp_offsets,exp);
regexp(regexp_exist,str);
printf(regexp(regexp_start));
printf(regexp(regexp_end));
}   
returns
6
12