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)
In reply to regex behaves differently in split vs substitute? by raygun
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |