in reply to Minimizing the amount of place holders on long identical regex

perl -lE "print '041424344454647484940414' =~ /(\d).?/g" 012345678901
The /g flag on regex means global search, so the regex will be applied several times on the string, starting each time from the end of the previous match. In list context (eg, when you affect the result to an array), this returns the list of captures (or, if none, the list of full matches). In my example above, I capture one digit, then try to match another character. If perl does manage to match that additional character (ie, it's not the end of the string), it will start looking after that position on the next attempt.

More info on that in perlretut and the description of the m// operator in perlop.

  • Comment on Re: Minimizing the amount of place holders on long identical regex
  • Download Code

Replies are listed 'Best First'.
Re^2: Minimizing the amount of place holders on long identical regex
by thanos1983 (Parson) on Jun 20, 2018 at 16:28 UTC

    Hello Eily,

    Thank you for your time and effort. The solution is working but in my case I can not use array as an output. This is because I can not use join to put the array in a string. I am using place holders as a solution to this problem.

    #!/usr/bin/env perl use strict; use warnings; use Data::Dumper; my $sample = "041424344454647484940414"; $sample =~ /([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])([ +0-9])([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])( +[0-9])([0-9])([0-9])([0-9])([0-9])([0-9])/; # 24 times the same patte +rn print "$1$3$5$7$9$11$13$15$17$19$21$23\n"; my $new = "041424344454647484940414"; $new =~ /(\d).?/g; print "$1$3$5$7$9$11$13$15$17$19$21$23\n"; my @array = $new =~ /(\d).?/g; print Dumper \@array; __END__ $ perl test.pl 012345678901 Use of uninitialized value $3 in concatenation (.) or string at test.p +l line 12. Use of uninitialized value $5 in concatenation (.) or string at test.p +l line 12. Use of uninitialized value $7 in concatenation (.) or string at test.p +l line 12. Use of uninitialized value $9 in concatenation (.) or string at test.p +l line 12. Use of uninitialized value $11 in concatenation (.) or string at test. +pl line 12. Use of uninitialized value $13 in concatenation (.) or string at test. +pl line 12. Use of uninitialized value $15 in concatenation (.) or string at test. +pl line 12. Use of uninitialized value $17 in concatenation (.) or string at test. +pl line 12. Use of uninitialized value $19 in concatenation (.) or string at test. +pl line 12. Use of uninitialized value $21 in concatenation (.) or string at test. +pl line 12. Use of uninitialized value $23 in concatenation (.) or string at test. +pl line 12. 0 $VAR1 = [ '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '1' ];

    Thanks again for your time and effort.

    Seeking for Perl wisdom...on the process of learning...not there...yet!