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”]
filter!(r".tif",a)

MethodError: objects of type Regex are not callable

1 Like

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

4 Likes

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

1 Like

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
1 Like