替换 R 中字符串中第一次出现的字符
Replace the first occurrence of a character within a string in R
我正在处理很多字符串。我意识到我可以使用 read.table()
阅读它们,但我必须事先清理它们。
我有这样的一般结构:
Request(123): \n Element1: 123123 \n Element2: 456456
我只想删除第一次出现的分号 :
,而不是其余的。
Request(123) \n Element1: 123123 \n Element2: 456456
让第一个字符串存储在test
中。阅读几个线程后,我尝试了 .*
:
gsub(pattern = ".*:", replacement = "", x = test)
我知道你可以在 上使用问号,但我无法让它工作。
gsub
中的g
代表global,表示匹配所有出现的地方。如果您使用 sub
而不是 gsub
,则只会匹配和替换第一个匹配项。有关详细信息,请参阅 ?gsub
,在 描述 中:
sub
and gsub
perform replacement of the first and all matches respectively.
而且,如果您只想替换冒号,您的模式应该只是 ":"
,".*:"
将匹配并替换最后一个冒号之前的所有内容。如果你想替换第一个冒号之前的所有内容,使用 sub
和 ?
使 *
不贪心就可以了。
x = "Request(123): \n Element1: 123123 \n Element2: 456456"
## match everything up through last colon
sub(".*:", "", x)
# [1] " 456456"
## not greedy, match everything up through first colon
sub(".*?:", "", x)
# [1] " \n Element1: 123123 \n Element2: 456456"
## match first colon only
## since we don't need regex here, fixed = TRUE will speed things up
sub(":", "", x, fixed = TRUE)
#[1] "Request(123) \n Element1: 123123 \n Element2: 456456"
## compare to gsub, match every colon
gsub(":", "", x, fixed = TRUE)
# [1] "Request(123) \n Element1 123123 \n Element2 456456"
我正在处理很多字符串。我意识到我可以使用 read.table()
阅读它们,但我必须事先清理它们。
我有这样的一般结构:
Request(123): \n Element1: 123123 \n Element2: 456456
我只想删除第一次出现的分号 :
,而不是其余的。
Request(123) \n Element1: 123123 \n Element2: 456456
让第一个字符串存储在test
中。阅读几个线程后,我尝试了 .*
:
gsub(pattern = ".*:", replacement = "", x = test)
我知道你可以在
gsub
中的g
代表global,表示匹配所有出现的地方。如果您使用 sub
而不是 gsub
,则只会匹配和替换第一个匹配项。有关详细信息,请参阅 ?gsub
,在 描述 中:
sub
andgsub
perform replacement of the first and all matches respectively.
而且,如果您只想替换冒号,您的模式应该只是 ":"
,".*:"
将匹配并替换最后一个冒号之前的所有内容。如果你想替换第一个冒号之前的所有内容,使用 sub
和 ?
使 *
不贪心就可以了。
x = "Request(123): \n Element1: 123123 \n Element2: 456456"
## match everything up through last colon
sub(".*:", "", x)
# [1] " 456456"
## not greedy, match everything up through first colon
sub(".*?:", "", x)
# [1] " \n Element1: 123123 \n Element2: 456456"
## match first colon only
## since we don't need regex here, fixed = TRUE will speed things up
sub(":", "", x, fixed = TRUE)
#[1] "Request(123) \n Element1: 123123 \n Element2: 456456"
## compare to gsub, match every colon
gsub(":", "", x, fixed = TRUE)
# [1] "Request(123) \n Element1 123123 \n Element2 456456"