Solved

Doing a basic "search and replace" on phone numbers

Posted on 2015-02-12
12
66 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

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.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
awk problem filtering combo 12 28
RegEx Help - open to other simple suggestions 8 63
regx  exclude  pattern 6 55
t-sql split string into multiple rows 7 147
Whatever be the reason, if you are working on web development side,  you will need day-today validation codes like email validation, date validation , IP address validation, phone validation on any of the edit page or say at the time of registration…
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…

679 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