在字符串中出现第 n 次子字符串后替换/删除

编程入门 行业动态 更新时间:2024-10-28 06:24:41
本文介绍了在字符串中出现第 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 with s 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 次子字符串后替换/删除

本文发布于:2023-11-29 23:46:04,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1647999.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   次子

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!