2

I'm trying to do a very simple program to find matches with ismatch() function in Julia. Suppose my pattern is

e_pat = r".+@.+"

Then I make a list called input with some random elements:

input= ["pipo@gmail.com", 23, "trapo", "holi@gmail.com"]

Now I want to identify how many matches exist and then print them using e_pat as reference:

for i in input
    println(ismatch(e_pat, i)) && println(i)
end

With that code I just get "true" and the error displayed below:

true

TypeError: non-boolean (Void) used in boolean context

Stacktrace:
 [1] macro expansion at ./In[27]:4 [inlined]
 [2] anonymous at ./<missing>:?
 [3] include_string(::String, ::String) at ./loading.jl:522

What can I do in order to get the following?

"pipo@gmail.com"
"holi@gmail.com"

I read ismatch() documentation but found nothing useful. Any help will be much appreciated

Alejandro Carrera
  • 513
  • 1
  • 4
  • 14

1 Answers1

4

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 Strings:

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
HarmonicaMuse
  • 7,633
  • 37
  • 52
  • That was pretty useful. Thank you! Just one more question: How can I convert raw text into string? For example, using same list [pipo@gmail.com, 23, trapo, holi@gmail.com] but without " " strings with @. I'm asking this because I got an error when trying string.([pipo@gmail.com, 23, trapo, holi@gmail.com]). I know @ is for macros in Julia but I want to know if there's nothing I can do about it (maybe @ is a restricted symbol, for example). Thanks again! – Alejandro Carrera Feb 10 '18 at 06:14
  • Which version of Julia are you using @AlejandroCarrera? – HarmonicaMuse Feb 10 '18 at 07:59
  • `@` symbol is for macros, `23` is an `Int` literal but `pipo@gmail.com` without quotes is an error, that is **not** a literal of any kind, you need to enclose it with quotes so it becomes a string literal. If you have more specific doubts please create a new question about that. – HarmonicaMuse Feb 10 '18 at 08:02
  • [Literal (computer programming)](https://en.wikipedia.org/wiki/Literal_(computer_programming)) – HarmonicaMuse Feb 10 '18 at 08:10
  • I'm on 0.6.2 version. Thank you for your help and your advice! – Alejandro Carrera Feb 10 '18 at 20:32