Filtering arrays using Regex in Julia 1.0


How is this done in Julia 1.0? It works in 0.6.3.

a = [“1.tif”, “2.tif”, “1.doc”, “2.doc”]

MethodError: objects of type Regex are not callable


Make an anonymous function that checks if the regex occursin each element:

julia> a = ["1.tif", "2.tif", "1.doc", "2.doc"]
       filter!(s->occursin(r".tif", s),a)
2-element Array{String,1}:

(Using regexes directly as callables to check if they occurred in a string was deprecated in 0.7 because they should probably have demanded that the entire string match.)


Also, consider whether you want to match "report_tiffany.xls" or rather want `r"\.tif$".


Be aware also that . means any char in regex and you need to escape it.

julia> occursin(r".tif", "atif")

julia> occursin(r"\.tif", "atif")