匹配R中的卷曲引号

时间:2015-07-30 14:01:57

标签: regex r

我在使用UTF-8编码文本(小说,从Project Gutenberg下载)中匹配卷曲引号时遇到问题。我可以轻松匹配小写,其中引号用直撇号(')括起来,但是当引号括在弯引号中时,我似乎无法找到它们。我试图使用utf8ToInt()找到等价的整数,但它为卷曲标记返回NA

我四处搜索但在R中找不到任何有这个问题的人。任何帮助都会非常感激。

hardtimes<-readLines(con="http://www.gutenberg.org/files/786/786-0.txt",encoding="UTF-8")
tmp<-hardtimes[368] #sample line with quotes in it
str_detect(tmp,"‘") #returns FALSE
str_count(tmp,"‘") #returns 0
utf8ToInt("‘") #returns NA

1 个答案:

答案 0 :(得分:0)

有点作弊:可能是Windows问题。在Windows上测试。

library(stringr)
hardtimes <-
  readLines(con = "http://www.gutenberg.org/files/786/786-0.txt",encoding =
              "UTF-8")
tmp <- hardtimes[368] #sample line with quotes in it
tmp 
backtickInt <- utf8ToInt(tmp)[1]
backtick <- intToUtf8(backtickInt)

str_detect(tmp,backtick) #returns FALSE
str_count(tmp, backtick) #returns 0
utf8ToInt("‘") #returns NA