On Jan 28, 2013, at 11:57 AM, Angela Barone wrote: > Hello, > > I'm trying to abbreviate ordinals(?) that occur only in the middle of > an address and I'm having a problem. The line below works: > > $test_data =~ s/(\S) North (\S)/$1 N. $2/i; > > however, if the address is something like 901 North St., it abbreviates that > as well. I'm wanting it to work only on an address like > 53 North Benson St. -> 53 N. Benson St. > > Is there a way to know whether or not 'North' is a street name as > opposed to a direction, or am I asking too much? I was thinking of counting > the number of "words" between "North' and 'St.' (and if it's zero then don't > do it), but I wouldn't know how to do that or even if that's the best way to > do it.
You want to change 'North' to 'N.' unless it is followed directly by 'St'. This is known as a "negative lookahead assertion" and uses the (?!...) construct: $test_data =~ s/North (?!St)/N. /; Of course, you have more cases to handle. Here is a short program to get you started: #!/usr/bin/perl use strict; use warnings; my @a = ( '101 South Curtis Dr', '701 North St', '901 North Benson Rd', '802 West Ave', ); my @streets = qw( St Ave Dr Rd Ln ); my $streets = join('|',@streets); my @directions = qw( north south east west ); my $directions = join('|',@directions); for my $s ( @a ) { $s =~ s{ \b ($directions) \b \s+ (?!$streets) }{ uc substr($1,0,1) . '. ' }iex; print "$s\n"; } -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/