Group Scan Using RegEx

Returning to the following code is not what I expected:

iex(12)> Regex.scan(%r/(?<groupname>a)b(c)/g,"abcdabcd", capture: :groups)
[["a"], ["a"]]

There are 2 groups, but as a result, only the name appears. And also the group name is completely ignored.

If I use named_capture, I get the following:

iex(14)> Regex.named_captures(%r/(?<groupname>a)b(c)/g,"abcdabcd", capture: :groups)
[groupname: "a"]

Well, I think the result

Regex.scan(%r/(?<groupname>a)b(c)/g,"abcdabcd", capture: :groups)

should be either

[["a", "c"], ["a", "c"]]    #=> including "anonymous groups"

or

[[groupname: "a"], [groupname: "a"]] #=> include group name in the results

So my question is, why should I name groups in group captures when using Regex.scan so that they are completely ignored?

+4
source share
1 answer

:groups . , . :groups :named . , capture: :all_but_first. .

+4

Source: https://habr.com/ru/post/1527711/


All Articles