regex - R grep to match dot -
so have 2 strings mylist<-c('claim', 'cl.bi')
, when
grep('^cl\\.*', mylist)
it returns both 1 , 2. if do
grep('^cl\\.', mylist)
it return 2. why first match 'claim'
? happened period matching?
"^cl\\.*"
matches "claim"
because *
quantifier defined thusly (here quoting ?regex):
'*' preceding item matched 0 or more times.
"claim"
contains beginning of line, followed c
, followed l
, followed 0 (in case) or more dots, fulfilling requirements successful match.
if want match strings beginning cl.
, use one or more times quantifier, +
, this:
grep('^cl\\.+', mylist, value=true) # [1] "cl.bi"
Comments
Post a Comment