String variables can be modified using the search-and-replace string operator1, =, which takes a regular expression with a syntax similar to that expected by the shell command sed and applies it to the relevant string variable.2 In the following example, the first instance of the letter s in the string variable twister is replaced with the letters th:
pyxplot> twister="seven silver soda syphons"
pyxplot> twister = s/s/th/
pyxplot> print twister
theven silver soda syphons
Note that only the s (substitute) command of sed is implemented in PyXPlot. Any character can be used in place of the / characters in the above example, for example:
twister =~ s's'th'
Flags can be passed, as in sed or perl, to modify the precise behaviour of the regular expression. In the following example the g flag is used to perform a global search-and-replace of all instances of the letter s with the letters th:
pyxplot> twister="seven silver soda syphons"
pyxplot> twister = s/s/th/g
pyxplot> print twister
theven thilver thoda thyphonth
TableĀ 6.2 lists all of the regular expression flags recognised by the = operator.
g Replace all matches of the pattern; by default, only the first match is replaced. i Perform case-insensitive matching, such that expressions like [A-Z] will match lowercase letters, too. l Make m When specified, the pattern character s Make the . special character match any character at all, including a newline; without this flag, . will match anything except a newline. u Make x This flag allows the user to write regular expressions that look nicer. Whitespace within the pattern is ignored, except when in a character class or preceded by an un-escaped backslash. When a line contains a #, neither in a character class nor preceded by an un-escaped backslash, all characters from the left-most such # through to the end of the line are ignored.
w,
W,
b,
B,
s and
S dependent on the current locale.
matches the beginning of the string and the beginning of each line immediately following each newline. The pattern character $ matches at the end of the string and the end of each line immediately preceding each newline. By default,
matches only the beginning of the string, and $ only the end of the string and immediately before the newline, if present, at the end of the string.
w,
W,
b,
B,
s and
S dependent on the Unicode character properties database.
Footnotes