StringTools - Maple Programming Help

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

StringTools

 RegSub
 perform regular expression substitutions

 Calling Sequence RegSub( pattern, text, template )

Parameters

 pattern - string; regular expression text - string; text to use template - string; copy as output

Description

 • The RegSub( pattern, text, template ) function performs regular expression search and replace on text. RegSub tests whether the string text matches the regular expression pattern. If a match occurs, the string template is copied as the output, but substrings can be replaced with the portion of text that matched pattern as follows.
 The substring & is replaced with the portion of text that matched the entire regular expression pattern. If d is a single digit, then the substring \d is replaced with the substring of text matching the d-th parenthesized subexpression of pattern.
 If text does not match pattern, the value FAIL is returned.
 Note: RegSub is a very low-level procedure. For most applications, StringTools[RegSubs] is easier to use.
 • This function is part of the StringTools package, and so it can be used in the form RegSub(..) 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[RegSub](..).
 • 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.

Examples

 > $\mathrm{with}\left(\mathrm{StringTools}\right):$
 > $\mathrm{RegSub}\left("\left(\left(a\right)\right)","abc","gu\\1v\\2"\right)$
 ${"guava"}$ (1)
 > $\mathrm{RegSub}\left("\left(\left[abc\right]*\right)d","abbbcd","&-\\1"\right)$
 ${"abbbcd-abbbc"}$ (2)
 > $\mathrm{RegSub}\left("a\left(\left[bc\right]*\right)\left(c*d\right)","abcd","&-\\1-\\2"\right)$
 ${"abcd-bc-d"}$ (3)
 > $\mathrm{RegSub}\left("\left(bc+d|ef*g.|h?i\left(j+k\right)\right)","effgz","&-\\1-\\2"\right)$
 ${"effgz-effgz-"}$ (4)
 > $\mathrm{RegSub}\left("\left(bc+d|ef*g.|h?i\left(j|k\right)\right)","reffgz","&-\\1-\\2"\right)$
 ${"effgz-effgz-"}$ (5)
 > $\mathrm{RegSub}\left("\left(.*\right)c\left(anad\left[ai\right]\right)\left(.*\right)","Maple is canadian","\\1C\\2\\3"\right)$
 ${"Maple is Canadian"}$ (6)