【发布时间】:2014-05-21 11:57:03
【问题描述】:
嗨朋友们,我正在尝试在文件列表中搜索特定的关键字(以 txt 格式给出)。我正在使用正则表达式来检测和替换文件中关键字的出现。 下面是一个逗号分隔的关键字,我传递给它进行搜索。
library(stringi)
txt <- "automatically got activated,may be we download,network services,food quality is excellent"
应搜索“自动激活”并将其替换为 automatic_got_activated...“可能是我们下载”替换为“may_be_we_download”等等。
txt <- "automatically got activated,may be we download,network services,food quality is excellent"
for(i in 1:length(txt)) {
start <- head(strsplit(txt, split=" ")[[i]], 1) #finding the first word of the keyword
n <- stri_stats_latex(txt[i])[4] #number of words in the keyword
o <- tolower(regmatches(text, regexpr(paste0(start,"(?:[^a-zA-Z'-]+[a-zA-Z'-]+){0,",
n-1,"}"),text,ignore.case=TRUE))) #best match for keyword for the regex in the file
p <- which(!is.na(pmatch(txt, o))) #exact match for the keywords
}
【问题讨论】:
-
这个问题可能需要清理一下。您对问题的标题和描述不同。这也太大了;太多信息无法复制问题。试着把数据删减一点,让人们可以轻松地读入 R..
-
10 个这样的问题,SO 数据库将被关闭....
-
请考虑这样做以减少您的问题的大小:stackoverflow.com/questions/5963269/…
-
对不起,伙计们,我是新手..:(..感谢您分享这些链接
-
@OnkarK 我认为您需要更具体一点。我认为您正在尝试使用您编写的代码向我们展示您想要的东西,但该代码无法按您的预期工作。即使是写得很好的代码也很难理解一个问题。我建议你真正定义你所追求的(甚至可能是一个规则列表)。然后实际向我们展示您期望代码返回的示例输出。这是一个我无法仅用文字描述问题的示例,因此我也给出了所需的输出:stackoverflow.com/questions/22235288/…