6.2.5 Regular expressionsString 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 specified 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" 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" Table 6.3 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 w, W, b, B, s and S dependent on the current locale. m When specified, the pattern character 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. s Make the . special character match any character at all, including a newline; without this flag, . will match anything except a newline. u Make w, W, b, B, s and S dependent on the Unicode character properties database. 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. Footnotes
|