[英]Pattern match with grepl() function in R
I am confused on below result on pattern match using grepl()
function -我对使用grepl()
函数的以下模式匹配结果感到困惑 -
grepl("[0-9]{2}-[0-9]{2}-[0-9]{2}", "2010-04-09") # TRUE
grepl("[0-9]{4}-[0-9]{2}-[0-9]{2}", "2010-04-09") #TRUE
Shouldn't I expect the first result to be FALSE
?我不应该期望第一个结果是FALSE
吗?
Any pointer will be highly appreciated.任何指针都将受到高度赞赏。
The result is correct.结果是正确的。
grepl
is looking for the pattern of xx-xx-xx, where x is a digit, and that does appear in the first query. grepl
正在寻找 xx-xx-xx 的模式,其中 x 是一个数字,并且它确实出现在第一个查询中。 If you want to query starting from the beginning of the string, you can use the ^
symbol.如果要从字符串的开头开始查询,可以使用^
符号。
For example, if you were to run grepl("^[0-9]{2}-[0-9]{2}-[0-9]{2}", "2010-04-09")
, you'd get FALSE, but grepl("^[0-9]{4}-[0-9]{2}-[0-9]{2}", "2010-04-09")
would return TRUE.例如,如果您要运行grepl("^[0-9]{2}-[0-9]{2}-[0-9]{2}", "2010-04-09")
,您会得到 FALSE,但grepl("^[0-9]{4}-[0-9]{2}-[0-9]{2}", "2010-04-09")
会返回 TRUE。
PS: On the opposite end, $
indicates the end of the string. PS:在另一端, $
表示字符串的结尾。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.