用于搜索和替换文件中字符串的正则表达式 [英] Regular expression to search and replace a string in a file
问题描述
您好朋友,我正在尝试搜索文件列表中的特定关键字(以txt为单位).我正在使用正则表达式来检测并替换文件中关键字的出现. 下面是一个逗号分隔的关键字,我正在搜索这些关键字.
Hi friends I am trying to search particular keywords (given in txt) in a list of files.I am using a regular expression to detect and replace the occurrence of the keyword in a file. Below is a comma separated keywords that i am passing to be searched.
library(stringi)
txt <- "automatically got activated,may be we download,network services,food quality is excellent"
应该搜索自动激活"前的"Ex",然后将其替换为automatic_got_activated ...可能是我们下载"替换为"may_be_we_download",依此类推.
Ex "automatically got activated" should be searched and replaced by automatically_got_activated..."may be we download" replaced by "may_be_we_download" and so on.
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
}
推荐答案
我认为这可能是您想要的.
I think this may be what you're looking for.
> txt <- "automatically got activated,may be we download,network services,food quality is excellent"
可从以下位置搜索的虚构句子向量:
A made-up vector of sentences to search from:
> searchList <- c('This is a sentence that automatically got activated',
'may be we download some music tonight',
'I work in network services',
'food quality is excellent every time I go',
'New service entrance',
'full quantity is excellent')
完成这项工作的功能:
replace.keyword <- function(text, toSearch)
{
kw <- unlist(strsplit(txt, ','))
gs <- gsub('\\s', '_', kw)
sapply(seq(kw), function(i){
ul <- ifelse(grepl(kw[i], toSearch),
gsub(kw[i], gs[i], toSearch),
"")
ul[nzchar(ul)]
})
}
结果:
> replace.keyword(txt, searchList)
# [1] "This is a sentence that automatically_got_activated"
# [2] "may_be_we_download some music tonight"
# [3] "I work in network_services"
# [4] "food_quality_is_excellent every time I go"
让我知道它是否对您有用.
Let me know if it works for you.
这篇关于用于搜索和替换文件中字符串的正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!