in reply to parsing hmtl file with regex
I find it better and easier as far as the coding, to put the left hand side of the regex match into a list context and for example assign $catalog_num directly instead of fiddling with $1!
Update:#!/usr/bin/perl -w use strict; my @lines = ( 'Catalog Number: PAL1001', ' Catalog Number:PAL1001', 'Catalog Number: Catalog Number: PAL1001', 'Catalog Number: PAL1001Catalog Number: PAL1001', 'Cat Number: PAL1001Catalog', 'Catalog Number: 123PAL100'); foreach my $line (@lines) { my ($catalog_num) = $line =~ /^\s*Catalog Number:\s*([A-Za-z]+\d+)/ +; if ($catalog_num) { print "$catalog_num\n" } else { print "Bad Line!...$line\n"; } } __END__ PAL1001 PAL1001 Bad Line!...Catalog Number: Catalog Number: PAL1001 PAL1001 Bad Line!...Cat Number: PAL1001Catalog Bad Line!...Catalog Number: 123PAL100
In the above, a valid catalog_num cannot be a numeric zero and so I can just test "if ($catalog_num)" instead of "if (defined $catalog_num)".
|
|---|