raygun has asked for the wisdom of the Perl Monks concerning the following question:
O wise monks, this is probably one of those questions that has a simple, obvious answer that I'm just not seeing because I've been looking at it too long.
I have the following input:
mono-basic-2.10 mono-2.10.2-r1 mono-2.10.5I need to split each line into a package name and a version string. Because the hyphen may occur in either part, I need a regex that is smart enough to figure out which hyphen to split on.
This regex seems to work:
in that it turns the correct hyphen into a more obvious separator, outputting:foreach $line (<STDIN>) { chomp $line; $line =~ s/-(?=[^-]+(-r[0-9]+)?$)/ ==> /; print "$line\n"; }
mono-basic ==> 2.10 mono ==> 2.10.2-r1 mono ==> 2.10.5However, if I use the exact same regex in the split function:
it doesn't work:foreach $line (<STDIN>) { chomp $line; ($package, $ver) = split /-(?=[^-]+(-r[0-9]+)?$)/, $line; print "$package ==> $ver\n"; }
mono-basic ==> mono ==> -r1 mono ==>Giving split a LIMIT of 2 doesn't change the output; the actual version numbers are eaten, and only the trailing -r1 on the second line makes it into $ver. What is it about split's processing of the regex that is different from that of the substitute operator?
$ perl --version This is perl 5, version 12, subversion 3 (v5.12.3) built for i686-linux (with 13 registered patches, see perl -V for more detail)
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: regex behaves differently in split vs substitute?
by roboticus (Chancellor) on Oct 07, 2011 at 23:43 UTC | |
Re: regex behaves differently in split vs substitute?
by Marshall (Canon) on Oct 08, 2011 at 02:43 UTC | |
Re: regex behaves differently in split vs substitute?
by leslie (Pilgrim) on Oct 08, 2011 at 05:33 UTC | |
by Marshall (Canon) on Oct 08, 2011 at 12:58 UTC | |
by Anonymous Monk on Oct 08, 2011 at 13:36 UTC | |
by Anonymous Monk on Oct 08, 2011 at 13:19 UTC | |
by Marshall (Canon) on Oct 08, 2011 at 13:54 UTC | |
by Anonymous Monk on Oct 08, 2011 at 13:58 UTC | |
|
Back to
Seekers of Perl Wisdom