我想知道在Perl的正则表达式(访问由正则表达式捕获的子字符串)中,Julia的等价于$ 1,$ 2,...是什么吗?
例如
$s = "some random string";
$s =~ m/(o.e).+(i.g)/;
print $1, "\n", $2;
版画
ome
ing
如Julia手册所述,您可以RegexMatch
通过访问对象captures
字段或使用来从对象获取捕获的子字符串getindex
。例如:
julia> s = "some random string"
"some random string"
julia> m = match(r"(o.e).+(i.g)", s)
RegexMatch("ome random string", 1="ome", 2="ing")
julia> m[1] # access using getindex
"ome"
julia> m[2]
"ing"
julia> m.captures
2-element Array{Union{SubString{String}, Void},1}:
"ome"
"ing"
julia> m = match(r"om", s) # no captured substrings
RegexMatch("om")
julia> m.captures
0-element Array{Union{SubString{String}, Void},1}
julia> m = match(r"ox", s)
julia> typeof(m) # no match, m is nothing
Void
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句