in reply to Splitting 2 different patterns

Yet another alternative, the "defensive programming" style:

use Data::Dumper; my @intf_list = ("xe-1/2/0", "xe-2/2/0", "xf-3/2/a"); foreach my $myint (@intf_list) { my @outputlist = $myint=~/^(\w+)-(\d+)\/(\d+)\/(\d+)$/ or die "Couldn't match \"$myint\""; print Dumper(\@outputlist); } __END__ $VAR1 = ['xe', '1', '2', '0']; $VAR1 = ['xe', '2', '2', '0']; Couldn't match "xf-3/2/a" at - line 4.

Replies are listed 'Best First'.
Re^2: Splitting 2 different patterns
by karlgoethebier (Abbot) on Mar 17, 2015 at 12:03 UTC

    Perhaps a bit more "defensive": my @outputlist = $myint =~ m|(.+)-(.+)/(.+)/(.+)|;

    Regards, Karl

    «The Crux of the Biscuit is the Apostrophe»

      But  m|(.+)-(.+)/(.+)/(.+)| will also match  'xf-3/2/a' or even  '@-&-*&^-@#$/_)(/&^/%-$#?!' which seems less, not more, defensive.


      Give a man a fish:  <%-(-(-(-<

        It depends on what you're trying to defend: wrong data, or the maintainer?
        لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
        "...will also match 'xf-3/2/a' or even '@-&-*&^-@#$/_)(/&^/%-$#?!'"

        Sure, it will. But i have a strong faith in the good in people.

        But for peace in the monastry i eat humble pie - here the revised version (still 3 chars less to type):

        my @outputlist = $myint=~m|(\w+)-(\d+)/(\d+)/(\d+)|;

        Edit: Minor improvement of formatting.

        Best regards, Karl

        «The Crux of the Biscuit is the Apostrophe»