Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 74
  • Last Modified:

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
0
shawn857
Asked:
shawn857
  • 6
  • 6
1 Solution
 
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
 
ozoCommented:
Yes, but not a simple way.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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
 
ozoCommented:
This looks like a list of Delphi replacement strings http://www.regular-expressions.info/refreplacebackref.html
0
 
shawn857Author Commented:
Thanks Ozo.

Cheers
    Shawn
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 6
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now