Split rsplit and Regex

Hello!
Its a bug of rsplit or a feature of split?

str = "abcdefg"
split(str, r"cd") #fine
rsplit(str, r"cd") #error

Trace:

ERROR: MethodError: no method matching findlast(::Regex, ::String)
Closest candidates are:
  findlast(::AbstractChar, ::AbstractString) at strings/search.jl:400
  findlast(::AbstractString, ::AbstractString) at strings/search.jl:363
  findlast(::Function, ::Union{AbstractString, AbstractArray}) at array.jl:2099
  ...
Stacktrace:
 [1] _rsplit(str::String, splitter::Regex, limit::Int64, keepempty::Bool, strs::Vector{SubString{String}})
   @ Base .\strings\util.jl:492
 [2] rsplit(str::String, splitter::Regex; limit::Int64, keepempty::Bool)
   @ Base .\strings\util.jl:479
 [3] rsplit(str::String, splitter::Regex)
   @ Base .\strings\util.jl:479
 [4] top-level scope
   @ none:1

Julia 1.6.2

I think it’s a missing method for rsplit, or rather for findlast(::Regex, ::String) which is called by rsplit and subsequently findprev(::Regex, ::String, ::Int). I don’t know if PCRE (the regex library julia uses) has this function readily available. We call into the library for the findfirst/split version.

If you’d like to investigate further (you can find how findnext with regexes is implemented here) or open an issue about it, that would be very much appreciated!

3 Likes