R正则表达式删除除撇号以外的所有标点符号 [英] R regex remove all punctuation except apostrophe

查看:41
本文介绍了R正则表达式删除除撇号以外的所有标点符号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从字符串中删除除撇号以外的所有标点符号.这是我的 exastr2 <-

I'm trying to remove all punctuation from a string except apostrophes. Here's my exastr2 <-

str2 <- "this doesn't not have an apostrophe,.!@#$%^&*()"
gsub("[[:punct:,^\\']]"," ", str2 )
# [1] "this doesn't not have an apostrophe,.!@#$%^&*()"

我做错了什么?

推荐答案

否定先行断言"可用于在任何撇号被测试为标点符号之前从考虑中删除它们.

A "negative lookahead assertion" can be used to remove from consideration any apostrophes, before they are even tested for being punctuation characters.

gsub("(?!')[[:punct:]]", "", str2, perl=TRUE)
# [1] "this doesn't not have an apostrophe"

这篇关于R正则表达式删除除撇号以外的所有标点符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆