Findfirst(r"\d",tmp) works but : findlast(r"\d",tmp) ERROR: MethodError: no method matching findlast(::Regex, ::String)

What wrong ?

julia> tmp
"\":\"83 053 wyś"

julia> findfirst(r"\d",tmp)
4:4

julia> findlast(r"\d",tmp)
ERROR: MethodError: no method matching findlast(::Regex, ::String)
Closest candidates are:
  findlast(::Function, ::Union{AbstractString, AbstractArray}) at array.jl:2054
  findlast(::Function, ::Any) at array.jl:2046
  findlast(::AbstractString, ::AbstractString) at strings/search.jl:314
  ...
Stacktrace:
 [1] top-level scope at REPL[94]:100:
1 Like

The underlying PCRE library that Julia uses for Regex searches does not support reverse-order searches, unfortunately.

The workaround is to search the reversed string (using a manually reversed regex as needed) and then use the reverseind function to compute the indices in the original string.

julia> tmp = "\":\"83 053 wyś"
"\":\"83 053 wyś"

julia> findfirst(r"\d",tmp)
4:4

julia> findfirst(r"\d",reverse(tmp))
6:6

julia> reverseind(tmp, 6)
9

julia> tmp[9]
'3': ASCII/Unicode U+0033 (category Nd: Number, decimal digit)
4 Likes
Another workaround wouldbe to use match:
julia> tmp=["123","abc","12ab"]
3-element Array{String,1}:
 "123"
 "abc"
 "12ab"

julia> findlast(x -> match(r"\d",x) != Nothing , tmp)
3

But I didn’t check for performance differences, so I just let it there.

Ok, doesnt work for strings. Never mind.