Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Conditional Regex.Replace

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

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 64

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

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …

650 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