从字符串中提取最后一个大写 [英] Extract Last Upper cases from a string

查看:43
本文介绍了从字符串中提取最后一个大写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 R 中练习正则表达式.我想提取最后出现的两个大写字母.我试过了

I am practicing with regular expressions in R. I would like to extract the last occurrence of two upper case letters. I tried

>str_extract("kjhdjkaYY,","[:upper:][:upper:]")
[1] "YY"

而且它工作得很好.如果我想提取这种模式的最后一次出现怎么办.示例:

And it works perfectly fine. What if I would like to extract the last occurrence of such pattern. Example:

function("kKKjhdjkaYY,")
[1] "YY"

感谢您的帮助

推荐答案

我们可以使用 stringi 包中的 stri_extract_last_regex

We can use stri_extract_last_regex from stringi package

library(stringi)
stri_extract_last_regex("AAkjhdjkaYY,","[:upper:][:upper:]")
#[1] "YY"

<小时>

或者如果你想坚持使用stringr,我们可以提取所有匹配模式的组,然后使用tail


Or if you want to stick with stringr, we can extract all the groups which match the pattern and then get the last one using tail

library(stringr)
tail(str_extract_all("AAkjhdjkaYY,","[:upper:][:upper:]")[[1]], 1)
#[1] "YY"

这篇关于从字符串中提取最后一个大写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆