StringTools
RegSub
perform regular expression substitutions
Calling Sequence
Parameters
Description
Examples
RegSub( pattern, text, template )
pattern
-
string; regular expression
text
string; text to use
template
string; copy as output
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.
with⁡StringTools:
RegSub⁡((a)),abc,gu\\1v\\2
guava
RegSub⁡([abc]*)d,abbbcd,&-\\1
abbbcd-abbbc
RegSub⁡a([bc]*)(c*d),abcd,&-\\1-\\2
abcd-bc-d
RegSub⁡(bc+d$|ef*g.|h?i(j+k)),effgz,&-\\1-\\2
effgz-effgz-
RegSub⁡(bc+d$|ef*g.|h?i(j|k)),reffgz,&-\\1-\\2
RegSub⁡(.*)c(anad[ai])(.*),Maple is canadian,\\1C\\2\\3
Maple is Canadian
See Also
Regular_Expressions
string
StringTools[Escape]
StringTools[RegMatch]
StringTools[RegSubs]
StringTools[Substitute]
StringTools[SubstituteAll]
Download Help Document