I have a line
my $foo = 'one#two#three!four#five#six';
from which I want to extract parts that are separated by either # or ! . This is easy enough with split :
my @parts = split /#|!/, $foo;
An additional requirement is that I also need to fix the exclamation points. So I tried
my @parts = split /#|(!)/, $foo;
This, however, returns either an undef value or an exclamation point (which is also clearly indicated in the split specification).
So, I eliminated unnecessary undef values โโwith grep :
my @parts = grep { defined } split /#|(!)/, $foo;
It does what I want.
But I was wondering if I can change the regex so that I don't have to call grep as well.
source share