After learning how to pass regexes as arguments, I've tried to build my first regex using a sub, and I'm stuck once more. Sorry for the complex rules below, I've made my best to simplify them. I need at least some clues how to approach this problem.
The regex should consist of alternations, each of them consisting of left, middle and right, where left and right should come in pairs and the variant of middle depends on which right is chosen.
An array of Pairs contains pairs of left and right:
my Pair @leftright =
A => 'a',
...
Z => 'z',
;
Middle variants are read from a hash:
my Regex %middle =
z => / foo /,
a => / bar /,
m => / twi /,
r => / bin /,
...
;
%middle<z> should be chosen if right is z, %middle<a> — if right is a, etc.
So, the resulting regex should be
my token word {
| A <%middle[a]> a
| Z <%middle[z]> z
| ...
}
or, more generally
my token word {
| <left=@leftright[0].key>
<middle=%middle{@leftright[0].value}>
<right=@leftright[0].value>
| (the same for index == 1)
| (the same for index == 2)
| (the same for index == 3)
...
}
and it should match Abara and Zfooz.
How to build token word (which can be used e.g. in a grammar) with a sub that will take every pair from @leftright, put the suitable %middle{} depending on the value of right and then combine it all into one regex?
my Regex sub sub_word(Pair @l_r, Regex %m) {
...
}
my token word {
<{sub_word(@leftright, %middle)}>
}
After the match I need to know the values of left, middle, and right:
"Abara" ~~ &word;
say join '|', $<left>, $<middle>, $<right> # A|bar|a
X, the middle is/ waldo /. But you haven't said what programmatic relationship is supposed to be detected betweenXandwaldo. (Granted, you wroteznotXand/ zfoo /not/ waldo /but that makes no difference, unless you are meaning that thezin/ zfoo /isn't just to aid human understanding but is also to be detected by the program. In which case, no, I don't think you can do that -- I don't think your program can introspectively know that the/ zfoo /pattern contains az.)word? Should the sub be used before the regex parsing starts to build a predefinedtoken, or should it be used within the regex parser? Can you give a simple example?regex. I'll reformulate this part, making ahash.@leftrightand%middle, and the pattern should be built before the parsing begins. If there were only one variant the pattern would be smth like<$left> <$middle> <$right>. Here it should be<@leftright[0].key> <%middle{@leftright[0].value}> <@leftright[0].value> | (the same for index == 1 | then 2, 3 etc)...So the problem is that I e.g. don't know how to concatenateregexeswith alternation|in a loop.