LaintalAy has asked for the wisdom of the Perl Monks concerning the following question:
Dear Monks, I'm on need of wisdom,
The problem is that I'm having problems trying to use regexes to retrieve matches that are defined as one/none or more. I'm trying with a simple file as example:
1 23 456 789 0123 456 2 24 456 789 0123 456 3 23 456 789 0123 456 4 23 456 789 0123 456 5 23 456 789 0123 456
And I was wondering if it's possible to parse and assign them to variables in just one step. The regex I was trying to use was something like
while (<$fd>) { my $regex = '^(?:(\d+)\s+)+(\d+)$'; (my ($d1, $d2, $d3, $d4, $d5, $d6) = $_) =~ m/$regex/; print "Line $.\n"; print "\t$d1\n"; }
It doesn't work as I'd expect. It matches, but only retrieves the last two elements because instead of getting an array of results for the (?: )+ regex part it stores only the last one.
I know a split would work without that much of a hassle but.. shouldn't be possible to do that just with a regex? I've tried different things without success and I haven't found any relevant example of this.
Thanks,
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Variable matching on a regex
by almut (Canon) on Jun 17, 2010 at 10:02 UTC | |
by LaintalAy (Sexton) on Jun 17, 2010 at 12:31 UTC | |
Re: Variable matching on a regex
by cdarke (Prior) on Jun 17, 2010 at 10:55 UTC | |
by LaintalAy (Sexton) on Jun 17, 2010 at 12:19 UTC | |
by johngg (Canon) on Jun 17, 2010 at 13:42 UTC | |
by SuicideJunkie (Vicar) on Jun 17, 2010 at 12:56 UTC | |
by LaintalAy (Sexton) on Jun 17, 2010 at 13:35 UTC | |
by Marshall (Canon) on Jun 17, 2010 at 17:17 UTC | |
by furry_marmot (Pilgrim) on Jun 17, 2010 at 20:01 UTC | |
by BrowserUk (Patriarch) on Jun 17, 2010 at 16:09 UTC | |
by JavaFan (Canon) on Jun 17, 2010 at 15:57 UTC | |
Re: Variable matching on a regex
by eric256 (Parson) on Jun 17, 2010 at 14:23 UTC | |
by almut (Canon) on Jun 17, 2010 at 14:47 UTC | |
by eric256 (Parson) on Jun 17, 2010 at 15:00 UTC | |
Re: Variable matching on a regex
by furry_marmot (Pilgrim) on Jun 17, 2010 at 20:39 UTC |