The following should work say if you want to change the area code from 406 to 302
 
s/\b406(\d{7})\b/302$1/g
 
Basically, it looks for a 10 digit number beginning with 406, captures the last 7 digits, and replaces it with 302 and the same captured 7 digits.  I've treated it is separated by a word boundary, but if that isn't the case, you may want to remove the \b and replace with whatever separator you do use between your phone number and the rest of your data.  This will not work if you have numbers in the format (406) 555-1212 or 302-555-1212 or any other but straight up 10 digits, so you may want to check for the other situations with like a
 
 s/\b\(?406\)? ?-?(\d{3})-?(\d{4})\b/302$1$2/g
 
where all those confusing question marks are simply looking for 0 or 1 instances of the other usual phone number formats.
----- Original Message -----
Sent: Wednesday, January 30, 2002 6:52 PM
Subject: How to search & replace only the first 3 digitsof a 10 digit #? Newbie asks!

What regular expression might I use to replace area codes in a telephone #(or just the first 3 digits of a number. I can't use just a simple search and replace. For example: 4062324064 The new # would change to ex:302233024.
I of course only want the area code or the first 3 digits to be searched.
 
I am brand new to this, yet I a already have a need to replace area codes on a list. Your help is apreciated.
 
Mike Charles

 

Reply via email to