Solved

Conditional Regex.Replace

Posted on 2006-06-26
4
1,060 Views
Last Modified: 2009-07-29
This article (http://www.experts-exchange.com/Programming/Q_21668801.html?query=format+phone+number+with+regex.replace&clearTAFilter=true) showed me how to use Regex.Replace to format a string as a phonenumber, which for the most part is great.  However, if there is no extension in the phone number I don't want there to be a trailing hiphen.  How do I adjust the regular expression so that the last hipen between the phone number suffix and the extension is only used if there is an extension present in the phone number?

Here's what I'm using:
string phoneNumber = "1234567890"
ResultString = Regex.Replace(phoneNumber, "^(\\d{3})(\\d{3})(\\d{4})(\\d*)$", "($1) $2-$3-$4");

Output:  "(123) 456-7890-"

I want the output to look like "(123) 456-7890"
or like "(123) 456-7890-1234" if an extesion exists.

Thanks in advance
0
Comment
Question by:orrin77
[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
  • 2
  • 2
4 Comments
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 16989061
Hi orrin77;

The replacement string is very limited in what you can do. Because of that limitation to do what you want is to do the following.

using System.Text.RegularExpressions;

      string phoneNumber = "1234567890";
      string ResultString = "";
      string pattern = @"^(\d{3})(\d{3})(\d{4})(\d*)$";
      string replace1 = "($1) $2-$3";
      string replace2 = "($1) $2-$3-$4";
      string repStr = "";

      // Check to see if phone number has an extension.
      if( phoneNumber.Length > 10 )
            repStr = replace2;            // Has extension.
      else
            repStr = replace1;            // Does not have extension.

      // Reformat the telephone number
      ResultString = Regex.Replace(phoneNumber,
            pattern, repStr);

      MessageBox.Show( ResultString );


Fernando
0
 

Author Comment

by:orrin77
ID: 16995510
Right, but what about the regular expression itself?  Can I use a look ahead, or something like that to test a condition?
0
 
LVL 63

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 16995762
The problem is not in the Regular Expression to do the search the problem is in the replacement sting and what you can use for it. These are the only meta characters that can be used in the replacement string.

$number          Substitutes the last substring matched by group number number (decimal).
${name}          Substitutes the last substring matched by a (?<name> ) group.
$$            Substitutes a single "$" literal.
$&            Substitutes a copy of the entire match itself.
$`             Substitutes all the text of the input string before the match.
$'             Substitutes all the text of the input string after the match.
$+            Substitutes the last group captured.
$_            Substitutes the entire input string.

All other character are used as themselves and non of the above will let you do a look ahead in the replacement string.
0
 

Author Comment

by:orrin77
ID: 16997097
Thank you Fernando
0

Featured Post

Business Impact of IT Communications

What are the business impacts of how well businesses communicate during an IT incident? Targeting, speed, and transparency all matter. Find out more in this infographic.

Question has a verified solution.

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

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

688 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