在Mac或Linux下的R中运行以下命令会产生预期结果,即希腊字母beta:
gsub("<U\\+[0-9A-F]{4}>", "\u03B2", "<U+03B2>")
"\u03B2"
Run Code Online (Sandbox Code Playgroud)
但是,在Windows下运行第一个命令会产生错误的结果,但第二个命令会提供正确的beta输出.我在Windows上尝试了3个版本的R(3.0.2,3.1.1和3.1.2).他们都一直打印出"错误"的结果.(由于我现在无法访问Windows,因此无法发布输出.)
另外,是否可以使用gsub将格式<U + FFFF>(忽略空间,如果没有它,网站不显示任何内容)转换为"\ uFFFF"?
非常感谢你.
UPDATE:
窃取了MrFlick的解决方案,如果一个句子中有多个Unicodes,我就会破解以下丑陋的解决方案.但是,修复非常难看,所以请随意发布改进.
test.string <- "This is a <U+03B1> <U+03B2> <U+03B2> <U+03B3> test <U+03B4> string."
trueunicode.hack <- function(string){
m <- gregexpr("<U\\+[0-9A-F]{4}>", string)
if(-1==m[[1]][1])
return(string)
codes <- unlist(regmatches(string, m))
replacements <- codes
N <- length(codes)
for(i in 1:N){
replacements[i] <- intToUtf8(strtoi(paste0("0x", substring(codes[i], 4, 7))))
}
# if the string doesn't start with a unicode, the copy its initial part
# until first occurrence of unicode
if(1!=m[[1]][1]){
y <- substring(string, 1, …Run Code Online (Sandbox Code Playgroud)