Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Repeatedly match a perl expression for a specific/multiple number of times?

For the given string example: <a><b><c><d><e><f><g> I'd like to write an expression that will repeatedly match the first 5 <(?)> tokens and put them in $1, $2, $3, $4 and $5.

The naive implementation would be of course: /<(?)><(?)><(?)><(?)><(?)>/
But back in the day I remember doing something like /(<(?)>:5)/ instead.

I'm having a hard time finding this syntax.
Can anyone help?

Thanks.

like image 684
AVIDeveloper Avatar asked Dec 13 '22 09:12

AVIDeveloper


1 Answers

perl -wE '$_="<a><b><c><d><e><f><g>"; say /<(.)>/g;'

Will give all the matches. It's just a matter of getting a slice:

my @tokens = (/<(.)>/g)[0 .. 4];
like image 156
TLP Avatar answered Jan 31 '23 09:01

TLP