StringTools - Maple Programming Help

Home : Support : Online Help : Programming : Names and Strings : StringTools Package : Pattern Matching : StringTools/RegMatch

StringTools

 RegMatch
 determine if a string matches a regular expression

 Calling Sequence RegMatch(pattern, text, options)

Parameters

 pattern - string; regular expression to match text - string; text to test options - (optional) name; subexpression substitution

Description

 • 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](..).

Examples

 > $\mathrm{with}\left(\mathrm{StringTools}\right):$
 > $\mathrm{RegMatch}\left("ab+bc","abc"\right)$
 ${\mathrm{false}}$ (1)
 > $\mathrm{RegMatch}\left("ab+bc","abbc"\right)$
 ${\mathrm{true}}$ (2)
 > $\mathrm{RegMatch}\left("ab+bc","abbbc"\right)$
 ${\mathrm{true}}$ (3)
 > $\mathrm{RegMatch}\left("ab+bc","abbbbc"\right)$
 ${\mathrm{true}}$ (4)
 > $\mathrm{RegMatch}\left("^ab+bc","abbbbc"\right)$
 ${\mathrm{true}}$ (5)
 > $\mathrm{RegMatch}\left("^ab+bc","abbbbcx"\right)$
 ${\mathrm{false}}$ (6)
 > $\mathrm{RegMatch}\left("^ab+bc","abbbbcx"\right)$
 ${\mathrm{true}}$ (7)
 > $\mathrm{RegMatch}\left("ab*bc","abc"\right)$
 ${\mathrm{true}}$ (8)
 > $\mathrm{RegMatch}\left("ab*bc","abbc"\right)$
 ${\mathrm{true}}$ (9)
 > $\mathrm{RegMatch}\left("ab*c","ac"\right)$
 ${\mathrm{true}}$ (10)
 > $\mathrm{RegMatch}\left("ab+c","ac"\right)$
 ${\mathrm{false}}$ (11)
 > $\mathrm{RegMatch}\left("ab|cd","abcd"\right)$
 ${\mathrm{true}}$ (12)
 > $\mathrm{RegMatch}\left("Canad\left(a|ian\right)","Canada"\right)$
 ${\mathrm{true}}$ (13)
 > $\mathrm{RegMatch}\left("Canad\left(a|ian\right)","Canadian"\right)$
 ${\mathrm{true}}$ (14)
 > $\mathrm{RegMatch}\left("Canad\left(a|ian\right)","Australian"\right)$
 ${\mathrm{false}}$ (15)
 > $\mathrm{RegMatch}\left(".*\left(a|ian\right)","Australian"\right)$
 ${\mathrm{true}}$ (16)

The following is a Maple implementation of the `grep' text searching tool.

 > $\mathrm{grep}≔\left(\mathrm{pattern},\mathrm{filename}\right)→\mathrm{select}\left(\mathrm{curry}\left(\mathrm{RegMatch},\mathrm{pattern}\right),\mathrm{Split}\left(\mathrm{readbytes}\left(\mathrm{filename},\mathrm{∞},'\mathrm{TEXT}'\right),""\right)\right):$
 > $\mathrm{grep}\left(\mathrm{nobody},"/etc/passwd"\right)$
 $\left[{"nobody:x:65534:65534:nobody:/var/empty:/bin/false"}\right]$ (17)

In the following example, the matching string and substrings are assigned to names.

 > $\mathrm{RegMatch}\left(".*\left(north|south\right).*\left(left|right\right).*","Drive north on the highway. Turn left at the third stoplight.",\mathrm{directions},\mathrm{CompassDirection},\mathrm{TurnDirection}\right)$
 ${\mathrm{true}}$ (18)
 > $\mathrm{directions};$$\mathrm{CompassDirection};$$\mathrm{TurnDirection}$
 ${"Drive north on the highway. Turn left at the third stoplight."}$
 ${"north"}$
 ${"left"}$ (19)