elvish icon indicating copy to clipboard operation
elvish copied to clipboard

Support named capture groups in the result of re:find

Open SitiSchu opened this issue 6 years ago • 4 comments

Example: (?P<name>regex) then instead of accessing it using the index one could use name to access it

SitiSchu avatar Jul 13 '19 23:07 SitiSchu

This seems to have been fixed since this issue was opened or the issue is incorrect with respect to being unable to use named matches due to a misunderstanding about how to use that feature. This works for me:

> x=abcd
> re:replace '(?P<me>bc)' '($me)($0)($1)($2)' $x
▶ 'a(bc)(bc)(bc)()d'

Note that the $me dereference worked and was equivalent to $1.

krader1961 avatar Sep 26 '20 04:09 krader1961

There is also the question of accessing the named capturing groups in the result of re:find.

xiaq avatar Oct 26 '20 11:10 xiaq

Go's regexp package doesn't have this functionality, but there is a way to do this "manually": https://stackoverflow.com/questions/20750843/using-named-matches-from-go-regex

xiaq avatar Oct 26 '20 11:10 xiaq

It appears I might have misunderstood what @SitiSchu was requesting. This illustrates why a good example for how someone can reproduce an issue is important. The solution in the stackoverflow question could be implemented by adding something like a &name key to the match object returned by re:find. Where the value of the &name key is a map whose keys correspond to each named subexpression and the value is a [&text=$t &start=$s &end=$e] map as found in the &groups list for each indexed subexpression.

krader1961 avatar Oct 28 '20 02:10 krader1961