> x<-c("01.mp3","invite.mp3")
> x[grep(x,pattern="[:digit:]")]
[1] "invite.mp3"
In the regular expression,Why i can not get "01.mp3"?
> x<-c("01.mp3","invite.mp3")
> x[grep(x,pattern="[:digit:]")]
[1] "invite.mp3"
In the regular expression,Why i can not get "01.mp3"?
If you want to get "01.mp3" because it consists of two digits and ".mp3", then you could do something like:
x<-c("01.mp3","invite.mp3")
x[grep(x,pattern="[0-9]{2}.mp3")]
I think what's happening here (and someone will presumably correct me), is that you are not actually matching what you think you are. You need to put the bracket list [:digit:]
inside brackets to match the list, otherwise you are matching the literal characters in :digit:
. You can see this by adding a third element to x
:
x<-c("01.mp3","invite.mp3", ":")
x[grep(pattern="[:digit:]", x = x)]
#[1] "invite.mp3" ":"
So [:digit]
will match :
as well. If instead, you use [[
:
x[grep(x,pattern="[[:digit:]]")]
#[1] "01.mp3" "invite.mp3"
you are then matching the class digit.