StringTools
RegMatch
determine if a string matches a regular expression
Calling Sequence
Parameters
Description
Examples
RegMatch(pattern, text, options)
pattern
-
string; regular expression to match
text
string; text to test
options
(optional) name; subexpression substitution
The RegMatch( pattern, text ) function determines whether a string text matches the regular expression pattern.
If the string text matches the regular expression specified by pattern, the value true is returned. If text does not match pattern, the value false is returned.
Note: If the pattern argument contains syntax errors, Maple may signal an error.
Up to nine names can be passed to RegMatch in the options parameter. Upon a successful match, the first of these symbols is assigned the substring of text that matches the entire regular expression pattern, and the remaining symbols are assigned the portions of text that match any parenthesized subexpressions, if any.
This function is part of the StringTools package, and so it can be used in the form RegMatch(..) only after executing the command with(StringTools). However, it can always be accessed through the long form of the command by using the form StringTools[RegMatch](..).
All of the StringTools package commands treat strings as (null-terminated) sequences of 8-bit (ASCII) characters. Thus, there is no support for multibyte character encodings, such as unicode encodings.
with⁡StringTools:
RegMatch⁡ab+bc,abc
false
RegMatch⁡ab+bc,abbc
true
RegMatch⁡ab+bc,abbbc
RegMatch⁡ab+bc,abbbbc
RegMatch⁡^ab+bc$,abbbbc
RegMatch⁡^ab+bc$,abbbbcx
RegMatch⁡^ab+bc,abbbbcx
RegMatch⁡ab*bc,abc
RegMatch⁡ab*bc,abbc
RegMatch⁡ab*c,ac
RegMatch⁡ab+c,ac
RegMatch⁡ab|cd,abcd
RegMatch⁡Canad(a|ian),Canada
RegMatch⁡Canad(a|ian),Canadian
RegMatch⁡Canad(a|ian),Australian
RegMatch⁡.*(a|ian)$,Australian
The following is a Maple implementation of the `grep' text searching tool.
grep≔pattern,filename↦select⁡curry⁡RegMatch,pattern,Split⁡readbytes⁡filename,∞,TEXT,\n:
grep⁡nobody,/etc/passwd
nobody:x:65534:65534:nobody:/nonexistent:/usr/sbin/nologin
In the following example, the matching string and substrings are assigned to names.
RegMatch⁡.*(north|south).*(left|right).*,Drive north on the highway. Turn left at the third stoplight.,directions,CompassDirection,TurnDirection
directions;CompassDirection;TurnDirection
Drive north on the highway. Turn left at the third stoplight.
north
left
See Also
curry
readbytes
Regular_Expressions
select
string
StringTools[Escape]
StringTools[RegSubs]
StringTools[Split]
Download Help Document