[英]Error when using ismatch() function with regex in Julia
I'm trying to do a very simple program to find matches with ismatch() function in Julia. 我正在尝试做一个非常简单的程序,以在Julia中使用ismatch()函数查找匹配项。 Suppose my pattern is
假设我的模式是
e_pat = r".+@.+"
Then I make a list called input with some random elements: 然后,我创建了一个名为input的列表,其中包含一些随机元素:
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: 现在,我想确定存在多少个匹配项,然后使用e_pat作为参考进行打印:
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”,并且错误显示如下:
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. 我阅读了ismatch()文档,但发现没有任何用处。 Any help will be much appreciated
任何帮助都感激不尽
The problem is that while this expression returns true
: 问题是,尽管此表达式返回
true
:
julia> @show ismatch(e_pat, "pipo@gmail.com");
ismatch(e_pat,"pipo@gmail.com") = true
Using println
, just prints true
but returns nothing
: 使用
println
,仅输出true
但不返回 nothing
:
julia> @show println(ismatch(e_pat, "pipo@gmail.com"));
true
println(ismatch(e_pat,"pipo@gmail.com")) = nothing
Which is of type Void
: 哪个是
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: 错误告诉您不能在布尔上下文中使用
Void
类型的对象( nothing
),只是在Julia中将Void
视为单例的实例:
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)
. 这是告诉您
ismatch
没有这样的方法,您不能将其与以下类型的参数组合使用: (Regex, Int)
。
You could do something like this instead to make sure all the objects are String
s: 您可以改为执行以下操作,以确保所有对象都是
String
:
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: 最后,您可以使用宏
@show
(打印一个表达式及其结果并最终返回结果)而不是println
函数(该结果打印结果但不返回 nothing
)来调试发生的情况:
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
: 因此,为了打印您的预期结果,只需删除左侧的
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.