The current answers are excellent, but let me be a bit more verbose in explaining the origin of the misunderstanding.
The main point is that here you're comparing a token that is part of a grammar with a standalone regex. They use the same language, regular expressions, but they are not the same. You can use a regex to match, substitute and extract information; the objective of a token is purely extracting information; from a string with a regular estructure, I want a part and just that part.
I assume you're insterested in the LUKE part, and that you are using <after
to kinda express "No, not what I'm interested this", or "Skip this, get me only the goods". Jonathan has already said one way, probably the best, to do so:
grammar MyGrammar {
token TOP {
<character>
}
token character {
\n \n <( LUKE
}
}
say MyGrammar.subparse("\n\nLUKE");
Will not only math, but also only capture LUKE:
「
LUKE」
character => 「LUKE
skipping over that. However, grammars don't match, they extract. So you probably want the separators to also be in the grammar, not worth the while to repeat them over and over. Besides, in general grammars are intended to be used top-down. So this will do:
grammar MyGrammar {
token TOP {
<separator><character>
}
token separator { \n \n }
token character { <[A..Z]>+ }
}
say MyGrammar.parse("\n\nLUKE");
The character
token is now more general (although maybe it coud use some whitespaces, I don't know. Again, maybe you're not interested in the separator. Just use a dot to ignore it. Just because you're not interested does not mean you don't have to parse it, and grammars give you a way of doing it:
grammar MyGrammar {
token TOP {
<.separator><character>
}
token separator { \n \n }
token character { <[A..Z]>+ }
}
say MyGrammar.parse("\n\nLUKE");
This one gives the same result:
「
LUKE」
character => 「LUKE」
At the end of the day, grammars and regexes have different use cases, and thus different solutions for the same objective. Thinking about them in the proper way gives you a hint on how to structure them.
<(
to match the pattern but not capture it, which does what my intention was withafter
– Fructiferous