#!/usr/bin/env perl use warnings; use strict; use Test::More; sub parse { my ($str) = @_; my @m = $str =~ m{ ^ (?\w+?) (?: (?\d) (?\w)? (?\w)? )? $ }x; note explain { $str => \%+ }; # debug output @m = map {$_//''} @m; # undef -> "" (optional) return \@m; } is_deeply parse("RS"), ["RS","","",""]; is_deeply parse("RC1XY"), ["RC","1","X","Y"]; is_deeply parse("RW12QW1X"), ["RW12QW", "1", "X", ""]; is_deeply parse("Sample1Repeat1A"), ["Sample1Repeat", "1", "A", ""]; is_deeply parse("Sample2Repeat2"), ["Sample2Repeat", "2", "", ""]; is_deeply parse("Sample3Repeat"), ["Sample3Repeat", "", "", ""]; is_deeply parse("4SampleRepeat"), ["4SampleRepeat", "", "", ""]; is_deeply parse("4SampleRepeat4"), ["4SampleRepeat", "4", "", ""]; is_deeply parse("5SampleRepeat5D"), ["5SampleRepeat", "5", "D", ""]; done_testing; #### # { # 'RS' => { # 'P_ROOTCODE' => 'RS' # } # } ok 1 # { # 'RC1XY' => { # 'DAY1' => '1', # 'P_MON_CODE' => 'X', # 'P_NEW_MON_CODE' => 'Y', # 'P_ROOTCODE' => 'RC' # } # } ok 2 # { # 'RW12QW1X' => { # 'DAY1' => '1', # 'P_MON_CODE' => 'X', # 'P_ROOTCODE' => 'RW12QW' # } # } ok 3 ... ok 9 1..9