From this code golf post with the relevant portion reproduced below.
I also don't know why I need
{,2}
instead of{,3}
but it seems to work. If anyone knows the answer to that, let me know.grepl('^M{,2}(C[MD]|D?C{,2})(X[CL]|L?X{,2})(I[XV]|V?I{,2})$',scan(,''))
For a simpler example:
rn <- c('X', 'MX', 'MMX', 'MMMX', 'MMMMX')
grepl('^M{,2}X$',rn)
[1] TRUE TRUE TRUE TRUE FALSE
Why does {,2}
find 3 or fewer instances of M
equivalent to {0,3}
? Furthermore, why does this work at all? The regex guides I've found only speak of a missing upper bound (like {3,}
) not a missing lower bound. If one has the perl=TRUE
option set, R reads all as false.
> grepl('^M{,2}X$',rn, perl=T)
[1] FALSE FALSE FALSE FALSE FALSE