I'd like to replace all the cases of the word Date
in a string unless it is Date()
(that is Date followed by parenthesis). Here's an example of a string and what I tried at first:
x <- c("frDate", "Date()", "Date", "Sys.Date()")
gsub("Date", paste("Date:", Sys.Date()), x)
What I get:
> gsub("Date", paste("Date:", Sys.Date()), x)
[1] "frDate: 2013-04-04" "Date: 2013-04-04()" "Date: 2013-04-04"
[4] "Sys.Date: 2013-04-04()"
What I'd like:
> gsub("Date", paste("Date:", Sys.Date()), x)
[1] "frDate: 2013-04-04" "Date()" "Date: 2013-04-04"
[4] "Sys.Date()"
I thought maybe I could learn from my previous question on the matter:
gsub("(?=[^Date\\(\\)[^Date]])", paste("Date:", Sys.Date()), x)
Not so good.
If someone has a better title to make this solution more general please change it so R regex users have a searchable means of handling exceptions.