On Mon, Nov 9, 2009 at 8:30 PM, tom smith <climbingpartn...@gmail.com>wrote:

>
> On Mon, Nov 9, 2009 at 10:56 AM, Jim Gibson <jimsgib...@gmail.com> wrote:
>
>> However, here is a shortened form using regular expression:
>>
>>  my @output = $line =~ m{ \G (..) .. }gx;
>>
>> Verify how either of these works when you do not have a multiple of 2
>> characters in your input.
>>
>>
> It has other problems too:
>
> use strict;
> use warnings;
> use 5.010;
>
> my $str = "ab--cd--ef";
> my @pieces = $str =~ /\G(..)../g;
> say "@pieces";
>
>
> The output is:
>
> ab cd
>
> Note that the last two characters 'ef' were not included in the result.
>  You need a multiple of 4 characters in the string for that regex to work
> correctly.
>
>

This works:

----
use strict;
use warnings;
use 5.010;

my $str = "ab--cd--ef";
my @pieces = $str =~ /\G(..)(?:..)?/gx;
say "@pieces";
----

The output is: ab cd ef.  The non-capturing parentheses (?:) allow you to
apply a ? to the group, yet not have the match for that group inserted in
the results list.

Reply via email to