【问题标题】:Removing all punctuation apart from single apostrophes and hyphens within words删除单词中除单个撇号和连字符之外的所有标点符号
【发布时间】:2014-07-15 15:28:52
【问题描述】:

我之前问过一个类似的问题,但这个问题要具体得多,并且需要与之前提供的解决方案不同的解决方案,所以我希望可以发布它。我只需要在文本中保留撇号和字内破折号(删除所有其他标点符号)。比如我想从str1中获取str2:

str1<-"I'm dash before word -word, dash &%$,. in-between word, two before word --word just dashes ------, between words word - word"
str2<-"I'm dash before word word dash in-between word two before word  word just dashes  between words word  word"

到目前为止我的解决方案,首先删除单词之间的破折号:
gsub(" - ", " ", str1)

然后留下字母和数字字符以及剩余的破折号
gsub("[^[:alnum:]['-]", " ", str1)

问题是,它不会删除彼此跟随的破折号,例如“--”在单词的开头和结尾加上破折号:“-word”或“word--”

【问题讨论】:

    标签: regex r gsub


    【解决方案1】:

    我认为这样做:

    gsub('( |^)-+|-+( |$)', '\\1', gsub("[^ [:alnum:]'-]", '', str1))
    #[1] "I'm dash before word word dash  in-between word two before word word just dashes  between words word  word"
    

    【讨论】:

    • 很好,谢谢。我理解这个'-+|-+'的意思是:如果破折号在一个单词之前或之后出现1次或多次,则替换它,但我认为“|”用于交替匹配(例如 /(e|d)n/ 匹配“en”和“dn”)。我想知道在你的解决方案中是否以类似的方式使用它,以及开头和结尾的空格是否有意义......
    • ' -+|-+ ' 表示“一个空格后跟 1 个或多个破折号或 1 个或多个破折号后跟一个空格”
    • 经过一些测试,我注意到我的文本中仍然有一些不受欢迎的破折号,我想我的问题不够精确。你能帮我吗?建议的行不会删除单词之前或之间的多个破折号,如此字符串中的:string3
    • @user3722736 我修改以处理开始/结束破折号。您可以添加另一个 gsub 以消除单词之间剩余的多个破折号:gsub('-+', '-', above_result)
    【解决方案2】:

    这是一种方法:

    gsub("([[:alnum:]][[:punct:]][[:alnum:]])|[[:punct:]]", "\\1", str1)
    # [1] "I'm dash before word word dash  in-between word two before word word just dashes  between words word  word"
    

    或者,更明确地说:

    gsub("([[:alnum:]]['-][[:alnum:]])|[[:punct:]]", "\\1", str1)
    

    同样的东西,略有不同/更短:

    gsub("(\\w['-]\\w)|[[:punct:]]", "\\1", str1, perl=TRUE)
    

    【讨论】:

    • 最后一个选项很棒,应该选为正确答案
    【解决方案3】:

    我建议

    x <- "I'm dash before word -word, dash &%$,. in-between word, two before word --word just dashes ------, between words word - word"
    gsub("\\b([-'])\\b|[[:punct:]]+", "\\1", x, perl=TRUE)
    # =>  "I'm dash before word word dash  in-between word two before word word just dashes  between words word  word"
    

    请参阅R demo。正则表达式是

    \b([-'])\b|[[:punct:]]+
    

    请参阅regex demo。详情:

    • \b([-'])\b - -' 用单词字符(字母、数字或 _)括起来(注意:如果您只想保留在字母之间,请改用 (?&lt;=\p{L})([-'])(?=\p{L})
    • | - 或
    • [[:punct:]]+ - 1 个或多个标点符号。

    要删除此替换后产生的任何前导/尾随和双空格字符,您可以使用

    res <- gsub("\\b([-'])\\b|[[:punct:]]+", "\\1", x, perl=TRUE)
    res <- trimws(gsub("\\s{2,}", " ", res))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-03-26
      • 2012-01-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-13
      • 2017-11-11
      相关资源
      最近更新 更多