Hi Jenda, I like your elegant solution. Could you talk thru your regex a bit more please? I like the mapping thing but dont see how the regex knows how to apply it? Is it something to do with the qr/?
joel -----Original Message----- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]]On Behalf Of Jenda Krynicky Sent: 25 June 2002 18:56 To: [EMAIL PROTECTED] Subject: Re: regex help? From: michael higgins <[EMAIL PROTECTED]> > The problem hanging me up right now is the fixed field length in the > .dbf is too short -- so I decided to shorten most strings with the > following regexes.( I also want to eliminate a 'second line' in the > .dbf, so we can use it for something else, this is why the field is > now too short.) So, I'm abbreviating the longer, common strings and > stuffing into one field > > The bit of code: > > my $street = "$add"." "."$secln"; > > $street=~s/,//; Do you really want to remove the first comma in the $street? > $street=~s/(\b)Apartment(\s)|(\b)Apt(\s)/$1Apt\.$2/i; 1) \b matches the 0 character long word boundary. Therefore it's useless to capture it. 2) Why do you use \b before the word and \s after? Wouldn't it be better to use it on both places? 3) It's silly to replace something by itself. 4) You only have to escape the dot in the regexp, not in the replacement string. 5) While it's unlikely that the word will appear several times in street name I think it would be better to use the /g option. $street=~s/\bApartment\b/Apt./gi; > $street=~s/(\b)Boulevard(\s)|(\b)Blvd(\s)/$1Blvd\.$2/i; > $street=~s/(\b)Street(\s)|(\b)St(\s)/$1St\.$2/i; > $street=~s/(\b)Drive(\s)|(\b)Dr(\s)/$1Dr\.$2/i; > $street=~s/(\b)Avenue(\s)|(\b)Ave(\s)/$1Ave\.$2/i; > $street=~s/(\b)Circle(\s)|(\b)Cir(\s)/$1Cir\.$2/i; > > Now, the problem is likely obvious: how do I perform all the necessary > substitutions on the same string? I'd do it like this : # set up the mapping %replace = ( Boulevard => 'Blvd', Street => 'St', Drive => 'Dr', Avenue => 'Ave', Circle => 'Cir', ); # prepare the regular expression $regexp = join '|', keys %replace; $regexp = qr/\b(?:$regexp)\b/i; ... # find&replace $street =~ s/$regexp/$replace{$1}./g; HTH, Jenda =========== [EMAIL PROTECTED] == http://Jenda.Krynicky.cz ========== There is a reason for living. There must be. I've seen it somewhere. It's just that in the mess on my table ... and in my brain I can't find it. --- me _______________________________________________ ActivePerl mailing list [EMAIL PROTECTED] To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs _______________________________________________ ActivePerl mailing list [EMAIL PROTECTED] To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
