JuliaLang / julia

The Julia Programming Language
https://julialang.org/
MIT License
45.7k stars 5.48k forks source link

findfirst , findlast and findall differences in needles #37350

Open oxinabox opened 4 years ago

oxinabox commented 4 years ago

findfirst, findlast and findall each support a different set of needles when the haystack is an AbstractString.

findfirst(::Union{Regex, AbstractString, Function, AbstractChar}, ::AbstractString)
# Everything

findall(::Union{Regex, AbstractString, Function}, ::AbstractString)
# No AbstractChar

findlast(::Union{AbstractString, Function, AbstractChar}, ::AbstractString)
# No Regex

We should make them consistent

nalimilan commented 4 years ago

See also findnext and findprev, which are respectively consistent with findfirst and findlast.

I'm not sure it's possible to get the last match of a regex with PCRE. That question may not be well-defined, as one can also write a regex to extract the last match of an expression, and pass that to findfirst. Of course as a brute-force method we could have findlast return last(findall(...)), but that's not a good implementation.

stevengj commented 4 years ago

Yes, PCRE doesn't support reverse-order searching. (You can simulate by reversing the string and searching with a reversed regex, but automatically constructing a reversed regex in general seems to be a hard problem.)