R中的正则表达式:找到确切的数字 [英] Regex in R: finding exact number

查看:17
本文介绍了R中的正则表达式:找到确切的数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是在R中

grep("AB22", c("AB22" ,"AB22","AB22" ,"AB22+3" ,"AB226AEM+1","AB22AEM+2") , value=T) 

给出了所有这些:AB22"、AB22"、AB22"、AB22+3"、AB226AEM+1"、AB22AEM+2"

gives all of them: "AB22","AB22", "AB22" ,"AB22+3" ,"AB226AEM+1" ,"AB22AEM+2"

但是,我只想要 "AB22","AB22","AB22" ,"AB22+3" ,AB22AEM+2" 即所有包含 AB22 而不是 AB226 或 2265...等的条目.

but, I want only "AB22","AB22","AB22" ,"AB22+3" ,AB22AEM+2" i.e. all the entries containing AB22 and not AB226 ot 2265...etc.

谢谢

推荐答案

词边界的任务锚点和/或否定前瞻断言:

grep("\\bAB22(?!\\d)", c("AB22" ,"AB22","AB22" ,"AB22+3" ,"AB226AEM+1","AB22AEM+2") , value=T, perl=TRUE);

(?!\d) 表示断言不可能匹配当前位置之后的数字".

(?!\d) means "Assert that it's impossible to match a digit after the current position".

这篇关于R中的正则表达式:找到确切的数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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