On 5/16/11 Mon May 16, 2011 3:44 PM, "Owen" <rc...@pcug.org.au> scribbled:
> I am trying to get all the 6 letter names in the second field in DATA > below, eg > > BARTON > DARWIN > DARWIN > > But the script below gives me all 6 letter and more entries. > > What I read says {6} means exactly 6. \S{6} will match any string containing 6 consecutive non-whitespace characters. It will also match any string containing more than 6 such characters, because any such string contains within it a substring of exactly six characters. Perl matches do not have to match the entire string. > > What is the correct RE? If you want exactly six characters, then you need to specify that any characters before or after the wanted six are not also members of the desired class. In your case, the easiest way is to anchor the match at the beginning and the end: $line[1] =~ /^\S{6}$/ If you were looking for word characters, e.g. \w, you could use the word boundary assertion metasymbol \b: $line[1] =~ /\b\w{6}\b/ That will not work if your names contain punctuation characters, e.g O'Reilly. More complex matches can use the negative lookahead and lookbehind constructs. > > I have solved the problem my using if (length($data[1]) == 6 ) but > would love to know the correct syntax for the RE > > > TIA > > > Owen > > > ================================================================= > > #!/usr/bin/perl > > use strict; > use warnings; > > while (<DATA>) { > my $line = $_; > > my @line = split /,/; > $line[1] =~ s /\"//g; > > print "$line[1]\n" if $line[1] =~ /\S{6}/; > } > > __DATA__ > "0200","AUSTRALIAN NATIONAL UNIVERSITY","ACT","PO Boxes" > "0221","BARTON","ACT","LVR Special Mailing" > "0800","DARWIN","NT",,"DARWIN DELIVERY CENTRE" > "0801","DARWIN","NT","GPO Boxes","DARWIN GPO DELIVERY ANNEXE" > "0804","PARAP","NT","PO Boxes","PARAP LPO" > "0810","ALAWA","NT",,"DARWIN DELIVERY CENTRE" > "0810","BRINKIN","NT",,"DARWIN DELIVERY CENTRE" > "0810","CASUARINA","NT",,"DARWIN DELIVERY CENTRE" > "0810","COCONUT GROVE","NT",,"DARWIN DELIVERY CENTRE" > > =============================================================== -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/