gsub(pattern, replacement, x, ignore.case = FALSE, perl = FALSE, fixed = FALSE, useBytes = FALSE) sub(pattern, replacement, x, ignore.case = FALSE, perl = FALSE, fixed = FALSE, useBytes = FALSE)
pattern | a regular expression pattern as used by regexpr. |
replacement |
the string (or vector of strings) to replace the parts of x
matching the pattern. If fixed is FALSE (the default), then
you can use "\n" in the replacement string (where n is
a digit) to mean to insert the n-th parenthesized subpattern here.
Because the backslash might have a special meaning in replacements when fixed is FALSE, you must use 2 backslashes to put a backslash in the replacement. (Because S uses 2 backslashes to represent 1 backslash, that means you must type 4 backslashes.) |
x | a vector of strings in which to do the replacement. |
ignore.case | a logical value. If TRUE, the case (upper or lower) of alphabetic characters is ignored so that, for example, the pattern "A" matches both "a" and "A". The default is FALSE. |
perl | a logical value. See regexpr for information about how it affects the intepretation of the pattern argument. |
fixed | a logical value. If FALSE (the default, the pattern is interpreted as a regular expression. If TRUE the pattern is interpreted as a string containing no special characters. |
useBytes | a logical value. If TRUE then the string arguments are treated as a simple sequence of bytes. If this is FALSE and any of the x or pattern or replacement strings have 'bytes' encoding (see Encoding), then useBytes is set to TRUE and a warning is generated. |
gsub | all matches are replaced. |
sub | only the first match is replaced. |
# Remove possible .xls suffix, any case sub("\\.xls$", "", c("Data.XLS", "Odd.xls.XLS"), ignore.case=TRUE) # [1] "Data" "Odd.xls"gsub("man", "MAN", "A man, another man, and a woman") # [1] "A MAN, another MAN, and a woMAN"
gsub("([-+]?[0-9]+)", "<INTEGER:\\1>", "10 packs of 6") # [1] "<INTEGER:10> packs of <INTEGER:6>"
cat(gsub("[\\/]", "\\\\", "C:/PROGRAM FILES\\TIBCO"), "\n") # C:\PROGRAM FILES\TIBCO