【问题标题】:How to remove ascii characters from a string in r如何从r中的字符串中删除ascii字符
【发布时间】:2019-06-13 11:37:10
【问题描述】:

也许我不了解 ascii 的细微差别,但我无法从字符串中删除编码。

输入字符串为:

mystring<-"complications:  noneco-morbidity:nil \\x0c\\\\xd6\\p__"

我想要的输出是:

"complications:  noneco-morbidity:nil __"

我的尝试是:

iconv(x, "latin1", "ASCII", sub = "")

但没有删除任何内容

【问题讨论】:

    标签: r ascii


    【解决方案1】:

    将以下模式用作带有gsub 的正则表达式:

    "[\\x00-\\x7F]+"
    

    此表达式匹配任何非 ASCII 字符,gsub 将其删除 (replacement="")

    例子:

    gsub(pattern = "[\\x00-\\x7F]+", replacement = "", "complications:  noneco-morbidity:nil \\x0c\\\\xd6\\p__")
    
    [1] "complications  noneco-morbiditynil cdp__"
    

    【讨论】:

    • 警告:这会删除: 字符,我会尝试看看是否有替代表达式不这样做。
    【解决方案2】:

    以下不是一个干净的解决方案。但仍然可能有用。

    gsub("x0c|xd6|\\p|\\\\","", mystring)
    

    【讨论】:

      猜你喜欢
      • 2012-01-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-01
      • 2023-03-18
      • 1970-01-01
      • 2010-12-04
      相关资源
      最近更新 更多