我正在尝试替换字符串中的某个字符但是由于这种模式是重复发生的,我曾想过如果它甚至可能使用RegExes迭代替换它。 例如,考虑一个链接http://www.bbc.co.uk/food/ingredients/by/letter/a
我想用'b'替换最后的'a',然后用'c','d'替换,直到'z'迭代。 使用RegEx可以很简单:
http:\/\/www\.bbc\.co\.uk\/food\/ingredients\/by\/letter\/[a-z]
这将验证字符串,但我想自动化链接生成过程。我对'R'很新,我想知道这样的事情是否可行。
答案 0 :(得分:3)
您可以使用paste0
创建如下链接列表:
l <- paste0("http://www.bbc.co.uk/food/ingredients/by/letter/", letters)
导致以下列表:
> l
[1] "http://www.bbc.co.uk/food/ingredients/by/letter/a"
[2] "http://www.bbc.co.uk/food/ingredients/by/letter/b"
[3] "http://www.bbc.co.uk/food/ingredients/by/letter/c"
[4] "http://www.bbc.co.uk/food/ingredients/by/letter/d"
[5] "http://www.bbc.co.uk/food/ingredients/by/letter/e"
[6] "http://www.bbc.co.uk/food/ingredients/by/letter/f"
[7] "http://www.bbc.co.uk/food/ingredients/by/letter/g"
[8] "http://www.bbc.co.uk/food/ingredients/by/letter/h"
[9] "http://www.bbc.co.uk/food/ingredients/by/letter/i"
[10] "http://www.bbc.co.uk/food/ingredients/by/letter/j"
[11] "http://www.bbc.co.uk/food/ingredients/by/letter/k"
[12] "http://www.bbc.co.uk/food/ingredients/by/letter/l"
[13] "http://www.bbc.co.uk/food/ingredients/by/letter/m"
[14] "http://www.bbc.co.uk/food/ingredients/by/letter/n"
[15] "http://www.bbc.co.uk/food/ingredients/by/letter/o"
[16] "http://www.bbc.co.uk/food/ingredients/by/letter/p"
[17] "http://www.bbc.co.uk/food/ingredients/by/letter/q"
[18] "http://www.bbc.co.uk/food/ingredients/by/letter/r"
[19] "http://www.bbc.co.uk/food/ingredients/by/letter/s"
[20] "http://www.bbc.co.uk/food/ingredients/by/letter/t"
[21] "http://www.bbc.co.uk/food/ingredients/by/letter/u"
[22] "http://www.bbc.co.uk/food/ingredients/by/letter/v"
[23] "http://www.bbc.co.uk/food/ingredients/by/letter/w"
[24] "http://www.bbc.co.uk/food/ingredients/by/letter/x"
[25] "http://www.bbc.co.uk/food/ingredients/by/letter/y"
[26] "http://www.bbc.co.uk/food/ingredients/by/letter/z"
答案 1 :(得分:3)
提取目录名称(即/ a之前的所有内容),然后使用内置变量letters
附加每个字母:
u <- "http://www.bbc.co.uk/food/ingredients/by/letter/a" # input string
file.path(dirname(u), letters)
给出了这个字符向量:
[1] "http://www.bbc.co.uk/food/ingredients/by/letter/a"
[2] "http://www.bbc.co.uk/food/ingredients/by/letter/b"
[3] "http://www.bbc.co.uk/food/ingredients/by/letter/c"
[4] "http://www.bbc.co.uk/food/ingredients/by/letter/d"
[5] "http://www.bbc.co.uk/food/ingredients/by/letter/e"
[6] "http://www.bbc.co.uk/food/ingredients/by/letter/f"
[7] "http://www.bbc.co.uk/food/ingredients/by/letter/g"
[8] "http://www.bbc.co.uk/food/ingredients/by/letter/h"
[9] "http://www.bbc.co.uk/food/ingredients/by/letter/i"
[10] "http://www.bbc.co.uk/food/ingredients/by/letter/j"
[11] "http://www.bbc.co.uk/food/ingredients/by/letter/k"
[12] "http://www.bbc.co.uk/food/ingredients/by/letter/l"
[13] "http://www.bbc.co.uk/food/ingredients/by/letter/m"
[14] "http://www.bbc.co.uk/food/ingredients/by/letter/n"
[15] "http://www.bbc.co.uk/food/ingredients/by/letter/o"
[16] "http://www.bbc.co.uk/food/ingredients/by/letter/p"
[17] "http://www.bbc.co.uk/food/ingredients/by/letter/q"
[18] "http://www.bbc.co.uk/food/ingredients/by/letter/r"
[19] "http://www.bbc.co.uk/food/ingredients/by/letter/s"
[20] "http://www.bbc.co.uk/food/ingredients/by/letter/t"
[21] "http://www.bbc.co.uk/food/ingredients/by/letter/u"
[22] "http://www.bbc.co.uk/food/ingredients/by/letter/v"
[23] "http://www.bbc.co.uk/food/ingredients/by/letter/w"
[24] "http://www.bbc.co.uk/food/ingredients/by/letter/x"
[25] "http://www.bbc.co.uk/food/ingredients/by/letter/y"
[26] "http://www.bbc.co.uk/food/ingredients/by/letter/z"
答案 2 :(得分:0)
另一种选择:
####