正则表达式捕获组的 Julia 语法是什么(例如 Perl 中的 $1)?
What is the Julia syntax for regex capture groups (e.g. $1 in Perl)?
我想知道 Julia 在 Perl 的正则表达式(访问由正则表达式捕获的子字符串)中等同于 $1、$2... 是什么?
例如
$s = "some random string";
$s =~ m/(o.e).+(i.g)/;
print , "\n", ;
打印
ome
ing
正如 Julia manual 所解释的那样,您可以通过访问 captures
字段或使用 getindex
从 RegexMatch
对象获取捕获的子字符串。例如:
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
我想知道 Julia 在 Perl 的正则表达式(访问由正则表达式捕获的子字符串)中等同于 $1、$2... 是什么?
例如
$s = "some random string";
$s =~ m/(o.e).+(i.g)/;
print , "\n", ;
打印
ome
ing
正如 Julia manual 所解释的那样,您可以通过访问 captures
字段或使用 getindex
从 RegexMatch
对象获取捕获的子字符串。例如:
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