替换字符串中所有出现的模式 [英] Replacing all occurrences of a pattern in a string
问题描述
当用字符串和字符玩游戏时,我常常用数字和矩阵运行R。我想分析一些将时间读入R的数据,如下所示:
Used to run R with numbers and matrix, when it comes to play with strings and characters I am lost. I want to analyze some data where the time is read into R as follow:
>my.time.char[1]
[1] "\"2011-10-05 15:55:00\""
我想最后得到一个仅包含以下内容的字符串:
I want to end up with a string containing only:
"2011-10-05 15:55:00"
使用sub()函数(我几乎不了解...),我得到了以下内容结果:
Using the function sub() (that i barely understand...), I got the following result:
> sub("(\")","",my.time.char[1])
[1] "2011-10-05 15:55:00\""
这更接近我要查找的格式,但是我仍然需要摆脱最后两个字符( \ )。
This is closer to the format i am looking for, but I still need to get rid of the two last characters (\"
).
推荐答案
的第二行?sub
解释:
sub和gsub分别替换第一个匹配项和所有匹配项。
sub and gsub perform replacement of the first and all matches respectively.
,它应该告诉您使用 gsub
来代替。
which should tell you to use gsub
instead.
这篇关于替换字符串中所有出现的模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!