R - 替换正则表达式匹配的最后一个实例以及之后的所有内容 [英] R - replace last instance of a regex match and everything afterwards
本文介绍了R - 替换正则表达式匹配的最后一个实例以及之后的所有内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用正则表达式来替换短语的最后一个实例(以及该短语之后的所有内容,可以是任何字符):
I'm trying to use a regex to replace the last instance of a phrase (and everything after that phrase, which could be any character):
stringi::stri_replace_last_regex("_AB:C-_ABCDEF_ABC:45_ABC:454:", "_ABC.*$", "CBA")
但是,我似乎无法让 refex 正常运行:
However, I can't seem to get the refex to function properly:
Input: "_AB:C-_ABCDEF_ABC:45_ABC:454:"
Actual output: "_AB:C-CBA"
Desired output: "_AB:C-_ABCDEF_ABC:45_CBA"
我也试过 gsub() 但没有奏效.
I have tried gsub() as well but that hasn't worked.
有什么想法我哪里出错了吗?
Any ideas where I'm going wrong?
推荐答案
一种解决方案是:
sub("(.*)_ABC.*", "\\1_CBA", Input)
[1] "_AB:C-_ABCDEF_ABC:45_CBA"
这篇关于R - 替换正则表达式匹配的最后一个实例以及之后的所有内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文