如何将整数正则表达式捕获转换为Julia 0.4中的整数数组?

时间:2015-08-28 14:19:11

标签: regex julia

我无法将整数正则表达式捕获值转换为Julia 0.4中的整数数组。

在Julia 0.3中,我可以用int()完成这个,基本上就像:

c = ["1", "2"] ;
int(c)

但是在Julia 0.4中,这适用于常规字符串:

julia> c = ["1", "2"] ;

julia> int(c)
2-element Array{Int64,1}:
 1
 2

但不适用于子串(即正则表达式捕获):

re_dcIV = r"^
([IV])(\S+)\s+
(\d+)\s+
(\d+)\s+
DC\s+
(\S+)\s*$"x ;

line = "V1 1 2 DC 1" ;

m = match( re_dcIV, line ) ;

c = m.captures ;
println( "$c\n" ) ;

nodes = int(c[3:4]) ;

产生:

julia> nodes = int(c[3:4]) ;
WARNING: int(x::AbstractArray) is deprecated, use round(Int64,x) instead.
 in depwarn at deprecated.jl:63
 in int at deprecated.jl:49
while loading no file, in expression starting on line 0
ERROR: MethodError: `round` has no method matching round(::Type{Int64}, ::SubString{UTF8String})
Closest candidates are:
  round{T<:Integer}(::Type{T<:Integer}, ::Integer)
  round{T<:Integer}(::Type{T<:Integer}, ::Float16)
  round{T<:Union{Signed,Unsigned}}(::Type{T<:Union{Signed,Unsigned}}, ::Base.MPFR.BigFloat)
  ...
 in round at floatfuncs.jl:72
 in int at deprecated.jl:51

捕获数组的类型是:

Union{SubString{UTF8String},Void}["V","1","1","2","1"]

我无法在此联盟野兽的子集上执行int(c [3:4])。有没有办法将这个子串数组转换为字符串数组,以便我可以运行int()?将此拼接转换为整数数组的最佳方法是什么?

1 个答案:

答案 0 :(得分:3)

最直接的方法似乎是使用mapparse

julia> map(x -> parse(Int, x), c[3:4])
2-element Array{Int64,1}:
 1
 2