Question

The return for the following code is not at all what I expected:

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

There are 2 groups, but only the named one appears in the result. And also the groupname 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 of

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 do I have to name groups in group captures when using Regex.scan just so they are completely ignored?

Was it helpful?

Solution

It happens the name :groups is misleading. It is not meant to be all groups but only the named ones. That's why we are renaming :groups to :named in upcoming releases. You can get the behaviour you want by using capture: :all_but_first. I will be documenting this option shortly.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top