match. Matching a pattern

^string.match[pattern]
^string.match[pattern][options]

The operator searches for a match of a
pattern in a string. Pattern could be a string with PCRE-Perl-compatible regular expression-or regex-object [3.4.0].
Some examples of PCRE are given in "Attachment 4: Perl Compatible Regular Expressions".

The following search
options may be used:

i-case-insensitive;
x-ignore "white space" characters and allow #comments till the end of the line;
s-regard $ as the end of the whole text (default);
m-regard $ as the end of the line, but not the whole text;
U-inverts the "greediness" of the quantifiers so that they are not greedy by default, but become greedy if followed by ?; [3.3.0]
g
-find not only the first, but all occurrences of the pattern;
n-return number of matches instead of table with search results; [3.2.2]
'-evaluate values for prematch, match, postmatch columns.

Characters
^ and $ are used in Parser's syntax, that is why if you want to include them in your pattern, they must be given as ^^ and ^$ respectively (see also Literals).

If option
g is specified, a table with the results of the match will be created with one row per each occurrence. If option g is not specified, a table with the results will contain only one record with first occurrence. If substring is not found, the result of operation will be empty table. If option n is specified, a number of matches will be returned instead of table.


A matches' table (object of class table) contain the next columns
1, 2, …, n, prematch, match, postmatch, where
prematch is the column with substring coming from the beginning of the string to the place where the pattern-matching substring was found
match is the column with the pattern-matching substring
postmatch is the column with the substring that comes after pattern-matching substring and up to the end of the entire string
1, 2, , n are the columns with pattern-matching substrings enclosed in round brackets, where n is number of the left bracket

Note 1: values for
prematch, match, postmatch columns are evaluated only if option ' is specified.
Note 2: values for
1, 2, , n are evaluated only if round brackets used in pattern.
Note 3: you can use (?:...) instead of (...) in pattern if you don't need some parts of matches in table with results

Examples

$str[www.parser.ru?user=admin]
^if(^str.match[\?.+]){
match found}{match not found}

The code will output:
match found

$str[www.parser.ru?user=admin]
$mtc[^str.match[(\?.+)][']]
^mtc.save[match.txt]


The example will create a file
match.txt, with the following table:

prematch
match
postmatch
1
www.parser.ru
?user=admin

?user=admin



Copyright © 1997–2021 Art. Lebedev Studio | http://www.artlebedev.com Last updated: 29.06.2009