Reputation: 93
I want to replace DM*13:01:01:02
with DM*13:01:01:01
. However my script also changes DM*11:01:01:01, DM*03:01:01:01, DM*01:01:01:01
to DM*13:01:01:01
. I do not want these to be changed
The script I use:
> papST$DM_c1 <-gsub("[DM*]\\d[13][:]\\d[01][:]\\d[01][:]\\d[02]", "*13:01:01:01", papST$DM_o1, perl = TRUE)
Upvotes: 0
Views: 42
Reputation: 60060
Based on the examples you have given, you don't really need to use any fancy regex features to do the specific replacement you have mentioned. The only thing you need to include in your pattern is a backslash so that *
doesn't get treated as a special character:
x = c("DM*13:01:01:02", "DM*11:01:01:01", "DM*03:01:01:01", "DM*01:01:01:01")
gsub("DM\\*13:01:01:02", "DM*13:01:01:01", x)
If there are more values that need replacing, like you want to replace all values ending in 02
, then you may need to bring in some of the "pattern matching" features in regular expressions, but it's important not to overcomplicate things.
For reference, to replace all 02
s at the end of your strings, you could use a simple regex that uses $
, which matches at the end of a string:
gsub("02$", "01", x)
Upvotes: 1