Filtering arrays using Regex in Julia 1.0


#1

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

a = [“1.tif”, “2.tif”, “1.doc”, “2.doc”]
filter!(r".tif",a)

MethodError: objects of type Regex are not callable


#2

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}:
 "1.tif"
 "2.tif"

(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.)


#3

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


#4

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

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

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