?
Solved

Conditional Regex.Replace

Posted on 2006-06-26
4
Medium Priority
?
1,062 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 2000 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

Command Line Tips and Tricks

The command line is a powerful tool at the disposal of every Linux user. Although Linux distros come with beautiful user interfaces, it's worthwhile to learn the command line because it allows you to do a number of things that you otherwise cannot do from the GUI.  

Question has a verified solution.

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

The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on 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