Solved

Doing a basic "search and replace" on phone numbers

Posted on 2015-02-12
12
60 Views
Last Modified: 2015-02-12
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
Comment
Question by:shawn857
  • 6
  • 6
12 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 40606709
#!/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
 

Author Comment

by:shawn857
ID: 40606714
Thanks ozo... is there any way to do this without using capture groups?

Thanks
   Shawn
0
 
LVL 84

Expert Comment

by:ozo
ID: 40606732
Yes, but not a simple way.
0
 

Author Comment

by:shawn857
ID: 40606757
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
 
LVL 84

Expert Comment

by:ozo
ID: 40606772
In the replace portion, meta-characters lose their meaning.
0
 

Author Comment

by:shawn857
ID: 40606775
OK, so the only syntax you have to work with is () (capture groups) and $ (dollar sign)... basically?

Shawn
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 84

Expert Comment

by:ozo
ID: 40606781
In perl, $ for scalar interpolation retains its meaning.  () for capture and $ for end of string lose their search meanings.
0
 

Author Comment

by:shawn857
ID: 40606797
is that the only syntax used in replace?
0
 
LVL 84

Expert Comment

by:ozo
ID: 40606809
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
 

Author Comment

by:shawn857
ID: 40606815
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
 
LVL 84

Accepted Solution

by:
ozo earned 500 total points
ID: 40606825
This looks like a list of Delphi replacement strings http://www.regular-expressions.info/refreplacebackref.html
0
 

Author Closing Comment

by:shawn857
ID: 40607312
Thanks Ozo.

Cheers
    Shawn
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

by Batuhan Cetin Regular expression is a language that we use to edit a string or retrieve sub-strings that meets specific rules from a text. A regular expression can be applied to a set of string variables. There are many RegEx engines for u…
I have been reconstructing a PHP-based application that has grown into a full blown interface system over the last ten years by a developer that has now gone into business for himself building websites. I am not incredibly fond of writing PHP code o…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now