在过滤和清理希伯来语文本时,我发现
gsub("[[:punct:]]", "", txt)
实际上删除了相关字符。该字符是“ק”,位于键盘上的“E”位置。有趣的是,R 中的 gsub 函数删除了“ק”字符,然后所有单词都变得困惑。有人知道为什么吗?
最佳答案
根据Regular Expressions as used in R :
Certain named classes of characters are predefined. Their interpretation depends on the locale (see locales); the interpretation below is that of the POSIX locale.
根据。到 POSIX 语言环境,[[:punct:]]
应该捕获 ! " # $ % & ' ( ) * + , - . / : ; < = > ? @ [ \ ] ^ _ ` { | } ~
。因此,您可能需要调整正则表达式以仅删除所需的字符:
txt <- "!\"#$%&'()*+,\\-./:;<=>?@[\\\\^\\]_`{|}~"
gsub("[\\\\!\"#$%&'()*+,./:;<=>?@[\\^\\]_`{|}~-]", "", txt, perl = T)
[1] ""
关于正则表达式删除punct删除R中的非标点字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30000002/