perl regex to replace a hash character

The humble #, also called the “hash” or “pound” character (at least here in the U.S.), can be quite useful. Or not. Here’s how to deal with it using a perl regex.

My colleague Mike recently enabled a new feature on our Google Search Appliance that displays a map of someone’s location by clicking a link on their address. Neat feature, but to work best we have to use a full address string since the existence and formatting of postal codes globally is nowehere near universal.

We had a problem right away because certain of our corporate street addresses used a hash (#) symbol in place of the old-fashioned “No.” at the beginning of certain street addresses. Like this:

#15 Simpson Road, Bangalore 555555, India

(yeah, there’s some confusion between the use of the LDAP ‘street’ vs ‘postaladdress’ attributes in some quarters)

Unfortunately, the stock map lookup routine pretty much chokes on that leading hash symbol. After fiddling around with it for awhile, I came up with a regex filter to replace that leading hash in all our street addresses before they hit the backend LDAP directory that the GSA crawls.

s/^\x23/No. /;

(\x23 is perl’s representation of the hex code for # in all standard character tables)

That’s all there really is to it. Here’s how I’m using this method in context:

if($street) {
   for($street) {
      s/^\x23/No. /;
   }
}
$entry->replace('street') =>$street;
This entry was posted in Systems Analysis on by .

About phil

My name is Phil Lembo. In my day job I’m an enterprise IT architect for a leading distribution and services company. The rest of my time I try to maintain a semi-normal family life in the suburbs of Raleigh, NC. E-mail me at philipATlembobrothersDOTcom. The opinions expressed here are entirely my own and not those of my employers, past, present or future (except where I quote others, who will need to accept responsibility for their own rants).