8

What or how is Raku recursive regex syntax and all match variable in Raku as on try

'hellohelloworldworld' ~~ m{ ^(h\w+?o) (?0) world  };
say "\n=$&"

seems to not work

Please help out solving these.

7
  • @raiph This question should have an answer regarding "publication of a match variable." Please consider reposting, thx. Commented Feb 1, 2022 at 21:46
  • @raiph yes but you're citing an answer to a question initially posted 3.5 years ago. I for one expect the compiler to have evolved since then, so that what may have been considered a bug back then has either 1) been enshrined in the docs, or 2) has been obviated. Which is to say...going forward, a 2022 updated answer from you regarding "Publication" of match variables by Rakudo would be most prized. Regards. Commented Feb 1, 2022 at 22:51
  • 2
    @jubilatious I think it was changed in the question, so not your mistake :-) Commented Feb 2, 2022 at 16:46
  • 1
    @jubilatious1 "Still hoping to confirm/refute ..." Publication of match variables is irrelevant to recursion. That's one of the reasons I deleted my answer. (I don't think you can see my answer. You need 10K+ reputation to see deleted answers. Commented Feb 13 at 16:39
  • 2
    Having reviewed this SO right now (due to you linking to it in your recent SO) I see my answer (deleted at the time I write this comment) contained the seeds of a good answer. It correctly demonstrates both general recursion and <~~> recursion. I think I will reinstate it with the mistakes removed and a fresh attempt at directly answering the original question added. I recall deleting my answer because I had decided .@codesections' answer was better. I now realize his answer is wrong. I'll add a comment below his answer to clarify that. Commented Feb 13 at 16:47

2 Answers 2

8

Raku has dedicated syntax for anonymous recursive regexes :<~~>.

Using this syntax, you could write the regex in your question as:

'hellohelloworldworld' ~~ m{ ^(h\w+?o) <~~>? world  };

say $/; # OUTPUT: «「hellohelloworld」␤
        #          0 => 「hellohello」␤»
Sign up to request clarification or add additional context in comments.

2 Comments

"I didn't plagiarize, honest teach!" :) (I prepared an edit of my answer covering self-recursion, and a bogus-but-maybe-interesting "Answer #3", hours ago. But I lost net connection (it's yoyoing today due to Storm Corrie), and then got waylaid, before publishing it (just now).)
Confused, because without <~~>? the code say $/ if 'hellohelloworldworld' ~~ m{ ^(h\w+?o) world }; still returns the same result. I'm on REPL version moar (2021.06).
3

The two answers I expected to see have already been posted, they are:

  1. " {} publication" of a match variable for use later within the same regex/matching operation (technically a backreference):
    > say $/ if 'hellohelloworldworld' ~~ m/ ^(h\w+?o) {} $0 world /;
    「hellohelloworld」
     0 => 「hello」
    > say $/ if 'hellohelloworldworld' ~~ m/ ^(h\w+?o) world /;
    「hellohelloworld」
     0 => 「hellohello」

and,

  1. use of Raku's dedicated " <~~> recursing-match" operator within the regex.

In true TMTOWTDI-spirit, there is however a third option, using Raku's :nd() adverb to achieve a sort of "poor-man's" recursion. Starting from the ['(' \w* ] grouping, you can successively pull out 「(bird」, 「(in」, and 「(nest」 from the input string (bird(in(nest))). Or all three at once (last example):

In the Raku REPL:

> my $nested = "(bird(in(nest)))";
(bird(in(nest)))
> say $nested;
(bird(in(nest)))
> say  $nested ~~ m:1st/ ['(' \w* ] /;
「(bird」
> say  $nested ~~ m:2nd/ ['(' \w* ] /;
「(in」
> say  $nested ~~ m:3rd/ ['(' \w* ] /;
「(nest」
> say  $nested ~~ m:nd(1..3)/ ['(' \w* ] /;
(「(bird」 「(in」 「(nest」)
>

Behind the scenes this is most likely using Raku's :position adverb or :continue adverb, in conjunction with Raku's $/.to match variable:

> say  $nested ~~ m/ ['(' \w* ] /;
「(bird」
> say  $nested ~~ m:pos($/.to)/ ['(' \w* ] / given $nested ~~ m/ ['(' \w* ] /;
「(in」
> say  $nested ~~ m:pos($/.to)/ ['(' \w* ] / given $nested ~~ (m/ ['(' \w* ] / && m:pos($/.to)/ ['(' \w* ] /);
「(nest」
> 

Again, Raku gives you a lot of different ways to approach the problem, which is one of the niceties of the language.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.