?
Solved

Doing a basic "search and replace" on phone numbers

Posted on 2015-02-12
12
Medium Priority
?
69 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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
 
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 2000 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Do you hate spam? I do, and I am willing to bet you do as well. I often wonder, though, "if people hate spam so much, why do they still post their email addresses on the web?" I'm not talking about a plain-text posting here. I am referring to the fa…
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…
Suggested Courses

771 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