R grepl in Julia

I am not trying to reinvent the wheel. It just searches for a function that searches for a string or string vector and returns true for each element in which a match is found. This is what I have tried so far.

grepl(x::String, y) = length(search(x, y)) > 0 grepl(x::Vector{String}, y) = length.(search(x, y)) .> 0 grepl(x::Vector{AbstractString}, y) = length.(search(x, y)) .> 0 

Usage example:

 v = string.('a':'z') x = rand(v, 100) .* rand(v, 100) .* rand(v, 100) grepl(convert(Vector{String}, x), "z") 

Well, this will be a working example if I can get my types to work correctly. Basically, I could use return to select only elements that have a "z" in them.

+5
source share
1 answer

Just use contains . At 0.6, you can use it directly with dot broadcasting:

 julia> contains.(["foo","bar","baz"],"ba") 3-element BitArray{1}: false true true 

At the beginning of 0.5, you can simply wrap the second argument in an array: contains.(["foo","bar","baz"],["ba"]) .

+9
source

Source: https://habr.com/ru/post/1266710/


All Articles