示例数据:
normalCase <- 'He said, "I am a test," very quickly.'
endCase <- 'This is a long quote, which we said, "Would never happen."'
shortCase <- 'A "quote" yo';
beginningCase <- '"I said this," he said quickly';
multipleCase <- 'When asked, "No," said Sam "I do not like green eggs and ham."'
testdata = c(normalCase,endCase,shortCase,beginningCase,multipleCase)
使用以下内容提取引文和字符缓冲区:
result <-function(testdata) {
str_extract_all(testdata, '[^\"]?{15}"[^\"]+"[^\"]?{15}')
}
extract <- sapply(testdata, FUN=result)
摘录是矩阵中的列表。但是,我希望提取是一个字符串,以后我可以作为列合并到数据帧。我该怎么转换呢?
答案 0 :(得分:1)
normalCase <- 'He said, "I am a test," very quickly.'
endCase <- 'This is a long quote, which we said, "Would never happen."'
shortCase <- 'A "quote" yo';
beginningCase <- '"I said this," he said quickly';
multipleCase <- 'When asked, "No," said Sam "I do not like green eggs and ham."'
testdata = c(normalCase,endCase,shortCase,beginningCase,multipleCase)
# extract quotations
gsub(pattern = "[^\"]*((?:\"[^\"]*\")|$)", replacement = "\\1 ", x = testdata)
[1] "\"I am a test,\" "
[2] "\"Would never happen.\" "
[3] "\"quote\" "
[4] "\"I said this,\" "
[5] "\"No,\" \"I do not like green eggs and ham.\" "
pattern = "[^\"]"
将匹配除双引号之外的任何字符pattern = "[^\"]*"
将匹配除双引号0或更多次以外的任何字符pattern = "\"[^\"]*\""
将匹配双引号,然后是任何
字符除了双引号0或更多次,然后另一个双
引用(即)引用pattern = "(?:\"[^\"]*\")"
将与引文匹配,但不会捕获
它pattern = "((?:\"[^\"]*\")|$)"
将与引号或endOfString匹配,
抓住它。请注意,这是我们捕获的第一个组replacement = "\\1 "
将替换为我们捕获的第一个组,后跟空格