determine if a string matches a regular expression - Maple Help

Online Help

All Products    Maple    MapleSim


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

withStringTools:

RegMatchab+bc,abc

false

(1)

RegMatchab+bc,abbc

true

(2)

RegMatchab+bc,abbbc

true

(3)

RegMatchab+bc,abbbbc

true

(4)

RegMatch^ab+bc$,abbbbc

true

(5)

RegMatch^ab+bc$,abbbbcx

false

(6)

RegMatch^ab+bc,abbbbcx

true

(7)

RegMatchab*bc,abc

true

(8)

RegMatchab*bc,abbc

true

(9)

RegMatchab*c,ac

true

(10)

RegMatchab+c,ac

false

(11)

RegMatchab|cd,abcd

true

(12)

RegMatchCanad(a|ian),Canada

true

(13)

RegMatchCanad(a|ian),Canadian

true

(14)

RegMatchCanad(a|ian),Australian

false

(15)

RegMatch.*(a|ian)$,Australian

true

(16)

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

grep:=pattern,filename→selectcurryRegMatch,pattern,Splitreadbytesfilename,∞,'TEXT', :

grepnobody,/etc/passwd

nobody:x:65534:65534:nobody:/var/empty:/bin/false

(17)

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

true

(18)

directions;CompassDirection;TurnDirection

Drive north on the highway. Turn left at the third stoplight.

north

left

(19)

See Also

curry, readbytes, Regular_Expressions, select, string, StringTools, StringTools[Escape], StringTools[RegSubs], StringTools[Split]


Download Help Document

Was this information helpful?



Please add your Comment (Optional)
E-mail Address (Optional)
What is ? This question helps us to combat spam