3. Match a string against a regular expression

[<<<] [>>>]

re::match(string,regexp [, replace])
re::m(string,regexp [, replace])

This function is the main regular expression match function. The first argument is the string that is to be matched against the regular expression. The second argument is the regular expression. If the string matches the regular expression the return value of the function is true, otherwise false. The regular expressions may contain parentheses inside it. If it does the substrings matching the regular expression part between the parentheses will be stored in a regular expression dollar array.

The substrings can be accessed via the function re::dollar() dollar.

If there is a replace string defined the return value is either false if the string is not matched by the regular expression; or the replace string itself. The replace string may contain $0, $1, ... $n literal that will be replaced by the actual substrings between the parentheses. This is the same way as Perl does.

$0 is the substring matched by the whole regular expression.

For example:

import re.bas

print "match result=",re::match("ahlma","h(.*)","haho $0 $1 $0 q") print n = re::n() print "number of ()s =",n print for i=0 to n print i,". " print re::dollar(i) print next i

will print
match result=haho hlma lma hlma q
number of ()s =1
0. hlma
1. lma
Note that the short for re::m exists in case you are a Perl knowing programmer, because this function is similar to the Perl operator =~ m/.
[<<<] [>>>]