在字符串中出现第 n 次子字符串后替换/删除 [英] Substitute/remove after nth occurrence of substring in string
本文介绍了在字符串中出现第 n 次子字符串后替换/删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想要 R 中 sub 的正则表达式来替换字符串中第 n 次出现;"之后的字符在该字符串中,其中 n 是传递给正则表达式的变量数.
I'd like a regex for sub in R to substitute the characters in a string which follow the nth occurrence of ";" in that string, where n is a variable number passed to the regex.
stringA="a; b; c; d; e; f; g; h; i; j;"
stringB<-sub("^(;){4}.*", "", stringA)
##---------------^My attempt at a regular expression here-------
期望的输出:
stringB
"a; b; c; d;"
推荐答案
您可以使用以下正则表达式:
You can use the following regex:
^((?:[^;]*;){4}).*
匹配:
^
- 字符串的开始((?:[^;]*;){4})
-(第 1 组)捕获包含 4(或您通过s
传递的任何数字)的子字符串变量)出现[^;]*
- 除了;
之外的 0 个或多个符号;
- 文字分号
^
- start of string((?:[^;]*;){4})
- (Group 1) captures a substring comprising 4 (or any number you pass withs
variable) occurrences of[^;]*
- 0 or more symbols other than;
;
- a literal semi-colon
在替换模式中使用反向引用
\\1
我们恢复结果中的前导子字符串.Using backreference
\\1
in the replacement pattern we restore the leading substring in the result.参见 IDEONE 演示(此处,限制阈值作为字符串传递):
See IDEONE demo (here, the limit threshold is passed as a string):
stringA="a; b; c; d; e; f; g; h; i; j;" s <- "4" stringB <- sub(sprintf("^((?:[^;]*;){%s}).*", s), "\\1", stringA) stringB ## "a; b; c; d;"
或者,如果你传递一个整数值
Or, if you pass an integer value
s <- 4 sub(sprintf("^((?:[^;]*;){%d}).*", s), "\\1", stringA)
参见另一个演示
这篇关于在字符串中出现第 n 次子字符串后替换/删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文