Splitting on the colon might be your best bet if the nature of your data is field-delimited, but you should also be able to achieve the same with:
my $src_string = 'aaa:bbb:ccc:ddd:eee:'; my @results = $src_string =~ /([^:]*):/g;
Hope this helps,
-v.
my $src_string = 'aaa:bbb:ccc:ddd:eee:'; my @results = split /:/, $src_string; pop @results;
Update: Oops, I missed the last line of the OP. Well, neither solution populates $1, $2, $3, so I guess neither answers the OP.
That does not work when the string doesn't contain any colon:
my $src_string = 'aaa'; my @results = $src_string =~ /([^:]*):/g; use Data::Dumper; print Dumper \@results; __END__ $VAR1 = [];
--
David Serrano
my $src_string = 'aaa:bbb:ccc:ddd:eee'; my @results = "$src_string:" =~ /([^:]*):/g;
If you can cope with an array, something as simple as /([^:]+)+/g should do the trick.
my $c='aaa:bbb';
my @arr = $c =~ /[^:]+/g;
use Data::Dumper; print Dumper \@arr;
__END__
$VAR1 = [
'aaa',
'bbb'
];
---
my $c='aaa:bbb:foo:bar';
my @arr = $c =~ /[^:]+/g;
use Data::Dumper; print Dumper \@arr;
__END__
$VAR1 = [
'aaa',
'bbb',
'foo',
'bar'
];
Update: Simplified the regex, as per [id://106949] suggestion.
--
David Serrano
my $string = 'aaa:bbb:ccc:ddd:eee:'; my $chunk = '([^:]+:)'; my $colons =()= $string =~ /:/g; my $regex = $chunk x $colons; $string =~ /$regex/; print "$1,$2,$3,$4,$5";
$_ = "aaa:bbb:ccc:ddd:eee:"; @_ = $_ =~ /(.+?:)/g; print "@_"
There was yet only one ([kwaping]) who correctly answered the OP's question.
Here is my solution to it:
my $string = 'aaa:bbb:ccc:ddd:eee:'; my $regex= $string; $regex=~ s/[^:]*:/([^:]*:)/g; $string =~ /$regex/; print "$1,$2,$3,$4,$5";
perlmonks.org content © perlmonks.org and Hue-Bond, ikegami, kwaping, rojam74, sh1tn, Skeeve, Velaki
prlmnks.org © 2006 edmund von der burg (eccles & toad)
v 0.03