stringfish icon indicating copy to clipboard operation
stringfish copied to clipboard

sf_start needs paranthesis

Open eribul opened this issue 4 months ago • 0 comments

If I slightly modify the example code from ?sf_starts and use a slightly more complicated pattern, such as b|a, I expect to get matches for all strings starting by either a or b, hence TRUE for alpha and beta but FALSE for gamma and delta. What I get is this, however:

> x <- c("alpha", "beta", "gamma", "delta", "epsilon")
+ stringfish::sf_starts(x, "b|a")
[1]  TRUE  TRUE  TRUE  TRUE FALSE

Looking at the source code for the function I see:

> stringfish::sf_starts
function (subject, pattern, ...) 
{
    pattern <- paste0("^", pattern)
    sf_grepl(subject, pattern, ...)
}

Adding ^ to my pattern would yield ^b|a and will thus match any pattern either starting with b or which has a anywhere. I would suggest chaning paste0("^", pattern) to paste0("^(", pattern, ")") which would (in my case) yield the more correct ^(b|a). Similair for sf_ends.

P.S. Thank you very much for a wonderful package! It is really awsome! :-)

eribul avatar Sep 24 '25 07:09 eribul