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

Accessing captures from another capture fails a match #6536

Closed
p6rt opened this issue Sep 18, 2017 · 5 comments
Closed

Accessing captures from another capture fails a match #6536

p6rt opened this issue Sep 18, 2017 · 5 comments
Labels
regex Regular expressions, pattern matching, user-defined grammars, tokens and rules

Comments

@p6rt
Copy link

p6rt commented Sep 18, 2017

Migrated from rt.perl.org#132120 (status was 'rejected')

Searchable as RT132120$

@p6rt
Copy link
Author

p6rt commented Sep 18, 2017

From @zoffixznet

IRC​: https://irclog.perlgeek.de/perl6/2017-09-18#i_15181575

We can use $0 here to refer to previous capture and regex matches​:
  m​: say "11" ~~ /(\d)[$0]/
  rakudo-moar 48a84d​: OUTPUT​: «「11」␤ 0 => 「1」␤»

But if we capture that match, the match fails​:
  m​: say "11" ~~ /(\d)([$0])/
  rakudo-moar 48a84d​: OUTPUT​: «Nil␤»

@p6rt
Copy link
Author

p6rt commented Sep 18, 2017

From @smls

On Mon, 18 Sep 2017 09​:00​:54 -0700, cpan@​zoffix.com wrote​:

IRC​: https://irclog.perlgeek.de/perl6/2017-09-18#i_15181575

We can use $0 here to refer to previous capture and regex matches​:
m​: say "11" ~~ /(\d)[$0]/
rakudo-moar 48a84d​: OUTPUT​: «「11」␤ 0 => 「1」␤»

But if we capture that match, the match fails​:
m​: say "11" ~~ /(\d)([$0])/
rakudo-moar 48a84d​: OUTPUT​: «Nil␤»

Not a bug.

Each `( )` capture represents a subpattern¹ that builds its own Match object, and inside the subpattern, `$/` refers to this subpattern's Match (which in your case doesn't have any captures) and not to the parent pattern's Match (which has the capture you meant to access).
(PS​: Remember that `$0` is just an alias for `$/[0]`.)

I don't think there's a built-in way to access a parent pattern's Match object - you'll have to manually pass on the object in a variable or similar, like you already discovered in the linked IRC log.

This ticket can IMO be closed as 'rejected', but I'll leave it open for now in case I missed something obvious or in case you want to turn it into an RFC or similar.


[1] http://design.perl6.org/S05.html#Subpattern_captures

@p6rt
Copy link
Author

p6rt commented Sep 18, 2017

The RT System itself - Status changed from 'new' to 'open'

@p6rt p6rt closed this as completed Sep 18, 2017
@p6rt
Copy link
Author

p6rt commented Sep 18, 2017

@zoffixznet - Status changed from 'open' to 'rejected'

@p6rt
Copy link
Author

p6rt commented Sep 18, 2017

From @zoffixznet

On Mon, 18 Sep 2017 09​:46​:50 -0700, smls75@​gmail.com wrote​:

This ticket can IMO be closed as 'rejected', but I'll leave it open
for now in case I missed something obvious or in case you want to turn
it into an RFC or similar.

---
[1] http://design.perl6.org/S05.html#Subpattern_captures

I just didn't know of this feature. Thanks for clarifying.

Rejecting

@p6rt p6rt added the regex Regular expressions, pattern matching, user-defined grammars, tokens and rules label Jan 5, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
regex Regular expressions, pattern matching, user-defined grammars, tokens and rules
Projects
None yet
Development

No branches or pull requests

1 participant