Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Captured strings of (.)(\g<1>) are same #157

Open
scivola opened this issue Feb 13, 2022 · 4 comments
Open

Captured strings of (.)(\g<1>) are same #157

scivola opened this issue Feb 13, 2022 · 4 comments

Comments

@scivola
Copy link

scivola commented Feb 13, 2022

Ruby code:

p /(.)(\g<1>)/.match("AB")
# => #<MatchData "AB" 1:"B" 2:"B">

I expected the following result:

# => #<MatchData "AB" 1:"A" 2:"B">
@tonco-miyazawa
Copy link

tonco-miyazawa commented Feb 13, 2022

It's probably specification.

Extend doc about subexpression calls #105
https://github.com/k-takata/Onigmo/pull/105/files

Regex engine "oniguruma" do the same behavior.

@tonco-miyazawa
Copy link

tonco-miyazawa commented Feb 13, 2022

p "ABXY".sub(/(.)\g<1>(.)\g<2>/,'[\1][\2]')
# result: "[B][Y]"

p "ABXY".sub(/(?<q>.)(?<q>.)(?<n>.)(?<n>.)/,'[\k<q>][\k<n>]')
# result: "[B][Y]"

p "ABXY".sub(/(.)\g<2>(.)\g<1>/,'[\1][\2]')
# result: "[Y][X]"

p "ABXY".sub(/(?<q>.)(?<n>.)(?<n>.)(?<q>.)/,'[\k<q>][\k<n>]')
# result: "[Y][X]"

https://github.com/kkos/oniguruma/blob/master/doc/RE
line 410

When backreferencing with a name that is assigned to more than one groups,
the last group with the name is checked first, if not matched then the
previous one with the name, and so on, until there is a match.

@tonco-miyazawa
Copy link

tonco-miyazawa commented Feb 13, 2022

/(.)(\g<1>)/
eq
/(?<m>.)(?<t>(?<m>.))/

MatchData1: \k<m>    # ==B
MatchData2: \k<t>    # ==B

@tonco-miyazawa
Copy link

tonco-miyazawa commented Feb 16, 2022

Named backrefs behave differently in Perl syntax #74

https://github.com/k-takata/Onigmo/blob/master/sample/simple.c
line:16,17,20 rewrote

Onigmo6.2.0 in ONIG_SYNTAX_PERL

"(.)((?1))\\k<1>"
"ABB"

[result]
match at 0
0: (0-3)
1: (1-2)
2: (1-2)
"(.)((?1))\\g{1}"
"ABB"

[result]
match at 0
0: (0-3)
1: (1-2)
2: (1-2)
"(.)((?1))\\k<1>"
"ABA"

[result] search fail
"(.)((?1))\\g{1}"
"ABA"

[result] search fail

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants