Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R's grepl in Julia

Tags:

julia

grepl

I am not trying to reinvent the wheel. Just looking for a function which searches a string or string vector and returns true for each element where the match is found. This is what I 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

Example usage:

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

Well, this would be a working example if I could get my types to work properly. Basically I could use the return to select only elements which have "z" in them.

like image 620
Francis Smart Avatar asked Apr 13 '17 18:04

Francis Smart


1 Answers

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

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

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

like image 58
mbauman Avatar answered Sep 28 '22 08:09

mbauman