RegexMatch to NamedTuple?

When parsing a string with regex like m = match(r"^(?<str_a>\S+) (?<str_b>\S+)$", "abc def") it is possible to get group values like m[:str_a] and so on. But how do I convert the match to namedtuple, (str_a = "abc", str_b = "def") in this case? I cannot find a way to do this in the documentation, or for that matter cannot find how to convert a regex match to any plain data structure like dict.

2 Likes

i check the source code, and the symbol calling uses a transformation to indexes. I mean, the natural representation of the RegexMatch is with number indices. i think that providing to the regex the symbols and building the dict or tuple from there is a possible solution, but its very interesting a convert to build a convert to that (maybe file a bug on github?). An option that can work is using those indices to build the tuple or dict. Here are some humble functions to build a dict or NamedTuple from a RegexMatch:

m1 = match(r"^(?<str_a>\S+) (?<str_b>\S+)$", "abc def")

function regex_to_dict(x::T) where T<:RegexMatch
return Dict{Int64,String}(i=>x.captures[i] for i = 1:length(x.captures))
end

function regex_to_dict(x::T,keys) where T<:RegexMatch
return Dict{eltype(keys),String}(keys[i]=>x.captures[i] for i = 1:length(x.captures))
end

function regex_to_ntuple(x::T) where T<:RegexMatch
    return NamedTuple{Tuple(collect(1:length(x.captures)))}(Tuple(x.captures))
end


function regex_to_ntuple(x::T,keys) where T<:RegexMatch
    return NamedTuple{Tuple(keys[1:length(x.captures)])}(Tuple(x.captures))
end

julia> regex_to_dict(m1,(:str_a,:str_b))
Dict{Symbol,String} with 2 entries:
  :str_a => "abc"
  :str_b => "def"

julia> regex_to_ntuple(m1,(:str_a,:str_b))
(str_a = "abc", str_b = "def")
4 Likes

In case anyone encounters this topic looking for a solution now, a recent Julia version made this conversion somewhat easier:

NamedTuple{Symbol.(Tuple(keys(m)))}(values(m))

to namedtuple, and

Dict(pairs(m))

to dict.

3 Likes

Every time I need this conversion, I either try to remember or lookup the solution above. So that it’s not needed anymore, I made a PR convert RegexMatch to Dict and NamedTuple by aplavin · Pull Request #50988 · JuliaLang/julia · GitHub that adds Dict/NamedTuple constructors converting RegexMatch to the corresponding container.
Hope this small but convenient functionality gets added soon!

1 Like