The problem is that while this expression returns true
:
julia> @show ismatch(e_pat, "pipo@gmail.com");
ismatch(e_pat,"pipo@gmail.com") = true
Using println
, just prints true
but returns nothing
:
julia> @show println(ismatch(e_pat, "pipo@gmail.com"));
true
println(ismatch(e_pat,"pipo@gmail.com")) = nothing
Which is of type Void
:
julia> typeof(nothing)
Void
And the error is telling you that you cant use an object of type Void
in a boolean context (nothing
) is just an instance of Void
treated like a singleton in Julia:
julia> nothing && true
ERROR: TypeError: non-boolean (Void) used in boolean context
After fixing that also notice that this is also another error:
julia> @show ismatch(e_pat, 42);
ERROR: MethodError: no method matching ismatch(::Regex, ::Int32)
Closest candidates are:
ismatch(::Regex, ::SubString{T<:AbstractString}) at regex.jl:151
ismatch(::Regex, ::SubString{T<:AbstractString}, ::Integer) at regex.jl:151
ismatch(::Regex, ::AbstractString) at regex.jl:145
...
This is telling you that ismatch
has no such method, you can't use it with a combination of arguments of types: (Regex, Int)
.
You could do something like this instead to make sure all the objects are String
s:
julia> input = string.(["pipo@gmail.com", 23, "trapo", "holi@gmail.com"])
4-element Array{String,1}:
"pipo@gmail.com"
"23"
"trapo"
"holi@gmail.com"
Finally, you could use the macro @show
(which prints an expression and its result and finally returns the result) instead of the println
function (which prints the result and returns nothing
, to debug whats going on:
julia> for i in input
@show(ismatch(e_pat, i)) && println(i)
end
ismatch(e_pat,i) = true
pipo@gmail.com
ismatch(e_pat,i) = false
ismatch(e_pat,i) = false
ismatch(e_pat,i) = true
holi@gmail.com
So in order to print your expected result just remove the left hand side println
:
julia> for i in input
ismatch(e_pat, i) && println(i)
end
pipo@gmail.com
holi@gmail.com
If you want to store them instead of printing them you could us an array comprehension instead:
julia> result = [str for str in input if ismatch(e_pat, str)]
2-element Array{String,1}:
"pipo@gmail.com"
"holi@gmail.com"
Or an indexing expression like this one:
julia> ismatch.(e_pat, input)
4-element BitArray{1}:
true
false
false
true
julia> result = input[ismatch.(e_pat, input)]
2-element Array{String,1}:
"pipo@gmail.com"
"holi@gmail.com"
That way you could print them later without having to repeat the computation:
julia> println.(result)
pipo@gmail.com
holi@gmail.com