Doing a basic "search and replace" on phone numbers

Hi, back with another basic regex question. I know just the rudiments of regex "search", but nothing about the "replace" aspect of it. Looked at an online tutorial regarding it:

http://www.regular-expressions.info/replacetutorial.html

... but found it quite hard to follow - offering very few examples. What I want to do isn't very complicated - basically just a phone number extraction/modification. For example:

Here is my source text string:

Bob, Smith, 256-871-9082

My regex search statement is :  \d\d\d[\-]\d\d\d[\-]\d\d\d\d

After grabbing the matching phone number, I'd like to do a "replace" on the match to modify it to look like so:  

+1-(256)*8719*082

Any suggestions please, on the regex "replace" statement I would need?

Thanks
    Shawn
shawn857Asked:
Who is Participating?
 
ozoConnect With a Mentor Commented:
This looks like a list of Delphi replacement strings http://www.regular-expressions.info/refreplacebackref.html
0
 
ozoCommented:
#!/usr/bin/perl
$_='Bob, Smith, 256-871-9082';
s/(\d\d\d)-(\d\d\d)-(\d)(\d\d\d)/+1-($1)*$2$3*$4/;
print;
0
 
shawn857Author Commented:
Thanks ozo... is there any way to do this without using capture groups?

Thanks
   Shawn
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
ozoCommented:
Yes, but not a simple way.
0
 
shawn857Author Commented:
Oh, I see. Ozo, is the syntax used in the replace portion of a regex completely different than the syntax in the search portion?

Thanks
   Shawn
0
 
ozoCommented:
In the replace portion, meta-characters lose their meaning.
0
 
shawn857Author Commented:
OK, so the only syntax you have to work with is () (capture groups) and $ (dollar sign)... basically?

Shawn
0
 
ozoCommented:
In perl, $ for scalar interpolation retains its meaning.  () for capture and $ for end of string lose their search meanings.
0
 
shawn857Author Commented:
is that the only syntax used in replace?
0
 
ozoCommented:
In perl, the replacement is just a qquoted string.
Variables that happen to have certain values, like $1, $2, are interpolated like they would be in any other qquoted string.
(with the /e or /ee modifier, the string is also evaluated)
0
 
shawn857Author Commented:
I don't know what you mean by interpolated Ozo. I'm not using Perl, I'm using a regex  engine for Delphi programming language.

Thanks
   Shawn
0
 
shawn857Author Commented:
Thanks Ozo.

Cheers
    Shawn
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.