in reply to Re: regex behaves differently in split vs substitute?
in thread regex behaves differently in split vs substitute?
Your if() statement is correct, a successful match will return a true/false value. However an assignment to $version like below will return a "defined" or "not defined" value which can also be used in an "if".
chomp if you like, but adding \s*$ includes \n in the regex (no need for chomp). chomp is "not expensive", but once we whip out the nuclear weapon of regex, asking it to throw away any trailing white space is no big deal.
use strict; use warnings; while (my $line = <DATA>) { my ($version) = $line =~ /^[a-z-]+(\d.*)\s*$/; print ">>$version<<\n" if $version; } =PRINTS: >>2.10<< >>2.10.2-r1<< >>2.10.5<< =cut __DATA__ mono-basic-2.10 mono-2.10.2-r1 mono-2.10.5
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: regex behaves differently in split vs substitute?
by Anonymous Monk on Oct 08, 2011 at 13:36 UTC | |
|
Re^3: regex behaves differently in split vs substitute?
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 | |
by AnomalousMonk (Archbishop) on Oct 08, 2011 at 18:43 UTC |