鉴于此字符向量
adjacency_list<>::edge_iterator
我尝试这段代码
columnsToKeep <- c("W","L","Customer Rate", "Diff% from Base",
"StoreOcc%", "COMPPS","Avail","Days in Unit",
"DSRC","Rec New Price", "Rec Rate Chg",
"intScheduledMoveOuts","TI30","BR1Yr",
"RLMI","NM7D","Last Rate Change %", "Occ%",
"Last Rate Change Amt", "BR", "MoveInRate",
"newRate",
"lengthOfStay", "mnyRentAtMoveIn",
"rentPriorToRateChange","mnyRentAtMoveOut","status")
产生
d<-columnsToKeep[grepl(" ", columnsToKeep)]
cat(gsub("(\\%?\\w+\\s+\\w+\\s*\\w*)", '`\\1`+', d))
但我想要这个
`Customer Rate`+ Diff% `from Base`+ `Days in Unit`+ `Rec New Price`+ `Rec Rate Chg`+ `Last Rate Change`+ % `Last Rate Change`+ Amt
很明显,我很难为%符号提供正确的正则表达式。
答案 0 :(得分:0)
我不太确定你正在尝试用正则表达式做什么,但看起来你想在d中的每个名字周围加上反引号然后用+加入它们。有两种方法可以实现:
cat(sapply(d, function(s) { paste0("`", s, "`") }), sep="+ ")
并且
cat(gsub("$", "`", gsub("^", "`", d)), sep="+ ")
答案 1 :(得分:-1)
我猜你只是在寻找在单词之间至少包含一个空格的条目。您可以尝试一下:"([\w%]+ [\w%]+(?: [\w%]+)*?)"
您似乎只是尝试匹配字母和%
,因此使用\w
可能会有点危险(它也匹配0-9
和_
)。您可以使用"([A-Za-z%]+ [A-Za-z%]+(?: [A-Za-z%]+)*?)"
另请注意,\s
并不仅仅意味着一个空格()。它是所有空白字符集,因此它也会匹配换行符,回车符和制表符。如果你想匹配一个空格,只需使用文字空间。