in reply to using regex to capture a string and an array
Check out perlre and the bit about greedy matching (in fact the whole thing is wirth a read). The (\d+) will capture one *or more* digits, and so will capture all the digits in one slurp. Also using the dot character is nasty as it will match anything and can lead to unexpected nasties...
Capturing an unknown number of things with regexes is difficult (c.f. known elements of unknown length), and so i would suggest keeping it simple and adding an intermediate step:
use strict; use warnings; while (<DATA>){ my $input = $_; if ( ## are you sure the format is correct? $input =~ m/^(\w{3}) ## match 3 alphanumerics at the start [^\d]* ## non digits in the middle (\d+) ## capture all the digits before \. ## an actual dot /x ){ my $site_code = $1; my @rs = split '', $2; ## split the digits up into an array print "Input : $input\n\$site_code : \'$site_code\'\n\@rs :\n\t", +(join "\n\t", @rs), "\n"; } else{ ## ... process alternately? print "input \'$input\' cannot be processed.\n"; } } __DATA__ uk1sxve01205.gfjgjf5.fdhd5 usasxve513.gfdhf4.hgfd4 how_did_this_get_here?
Prints :
Input : uk1sxve01205.gfjgjf5.fdhd5 $site_code : 'uk1' @rs : 0 1 2 0 5 Input : usasxve513.gfdhf4.hgfd4 $site_code : 'usa' @rs : 5 1 3 input 'how_did_this_get_here?' cannot be processed.
|
|---|