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.

1 Like


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))

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))

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

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

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")