我正在使用 R(XML 包)解析一个 xml。XML 有以下标记。
< <
> >
& &
Input Text:
My age is &gt; 65 years years.
output: My age is gt;65 years.
预期输出:我的年龄>65 岁。
How to get 2-step(1) for converting & into & (2) > into '>' ?
sub(">" , ">",
sub("(&)", "&",
"My age is &gt; 65 years.") )
[1] "My age is > 65 years."
你可以写一个这样的函数
batchgsub <- function(patternmatrix, string) {
for (i in 1:nrow(patternmatrix)) {
p = patternmatrix[i,1]
r = patternmatrix[i,2]
string <- gsub(p,r,string)
}
return(string)
}
并像这样指定要替换的模式
> pm
[,1] [,2]
[1,] "&" "&"
[2,] ">" ">"
然后,您可以根据需要多次“链接”替换。
> s <- "My age is &gt; 65 years."
> batchgsub(pm, s)
[1] "My age is > 65 years."