简体   繁体   English

使用 grepl() 函数在 R 中进行模式匹配

[英]Pattern matching in R using grepl() function

I have below pattern match -我有以下模式匹配 -

grepl("Close_[a-zA-Z]{0,}_{0,1}ASE_[a-zA-Z]{0,}_{0,1}", "Close_ASE_RS____")
#TRUE

R returns this as affirmative match. R 将此作为肯定匹配返回。 Shouldn't it be FALSE since I have "____" which has more than 1 repetition of "_" , but in my pattern I put only max 1 match through "_{0,1}"不应该是FALSE因为我有"____"有超过1重复的"_" ,但在我的模式中,我只通过"_{0,1}"放置了最多1匹配项

Any pointer why it is happening will be highly helpful.为什么会发生这种情况的任何指示都将非常有帮助。

Thanks,谢谢,

Try indicating the string ends after your initial pattern:尝试在初始模式后指示字符串结束:

grepl("Close_[a-zA-Z]{0,}_{0,1}ASE_[a-zA-Z]{0,}_{0,1}$", "Close_ASE_RS____")

# [1] FALSE

Otherwise you can put anything after the initial underscore following RS and it will match it.否则,您可以在RS后的初始下划线后放置任何内容,它会匹配它。

We can use word boundary我们可以使用词边界

grepl("Close_[a-zA-Z]{0,}_{0,1}ASE_[a-zA-Z]{0,}_{0,1}\\b", "Close_ASE_RS____")
#[1] FALSE

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM