473,699 Members | 2,145 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

a simple one for the Regex-perts =)

my regex for matching phone #'s:
\({0,1}(?<areac ode>[0-9]{3})\){0,1}( |-){0,1}(?<prefix >[0-9]{3})(
|-){0,1}(?<suffix >[0-9]{4})

which i then .Replace to become:
(${areacode}) ${prefix}-${suffix}

question: how do i fix the match regex so that it 'stops' after the suffix
of the phone # is matched? right now, it will return '(800) 555-1212345' if
i type in '8005551212345' - i just want '(800) 555-1212'.

as always, any help would be much appreciated! thanks!
Nov 19 '05 #1
2 1449
> my regex for matching phone #'s:
\({0,1}(?<areac ode>[0-9]{3})\){0,1}( |-){0,1}(?<prefix >[0-9]{3})(
|-){0,1}(?<suffix >[0-9]{4})


I am no RegEx[pert] :-) but I think you have to join your groups as one.
What is happening is that your <suffix> group is being repeated because
[0-9]{4} can be found twice in 12121234, see: [1212] and [1234]. If you can
join the entire expression as a single group, I think that /may/ fix your
problem.

HTH

~
Jeremy

Nov 19 '05 #2
Regex.Replace simply replaces the matched text with the replacement string.
The expression only matches '8005551212', so it replaces that with '(800)
555-1212'. The '345' does not participate in the match, so it is left
alone. If you want it to go away, you need to make it part of the match and
then just leave it out of the replacement. Simply add '.*' to the end of
your regex to match whatever is left over after the number is matched:

\({0,1}(?<areac ode>[0-9]{3})\){0,1}( |-){0,1}(?<prefix >[0-9]{3})(
|-){0,1}(?<suffix >[0-9]{4}).*
Brian Davis
www.knowdotnet.com

"K. Shier" <ks*****@spamAt YourOwnRisk.yah oo.com> wrote in message
news:%2******** ********@TK2MSF TNGP12.phx.gbl. ..
my regex for matching phone #'s:
\({0,1}(?<areac ode>[0-9]{3})\){0,1}( |-){0,1}(?<prefix >[0-9]{3})(
|-){0,1}(?<suffix >[0-9]{4})

which i then .Replace to become:
(${areacode}) ${prefix}-${suffix}

question: how do i fix the match regex so that it 'stops' after the suffix of the phone # is matched? right now, it will return '(800) 555-1212345' if i type in '8005551212345' - i just want '(800) 555-1212'.

as always, any help would be much appreciated! thanks!

Nov 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
55386
by: Howard Dean | last post by:
I have a string with several carriage returns in it. For example: "This is my test string" I wish to convert it to "This is my test string" (remove all carriage returns. Can someone tell me how to do this using the string.Replace function? Thanks!
0
1575
by: Daniel Perron | last post by:
Hi, I was trying to use Galois.Net to specify code generators and I had to look for some text template tool to simplify the syntax. I was looking for something similar to the syntax of an Asp.Net page; for example, a template file could look like the following: <%@ Assembly Name="ReturnString.dll" %> <html> <title>Testing generator</title>
2
8103
by: live your lives | last post by:
i am trying to validate a simple username textbox using RegularExpressionValidator: TextBox tbUserName = new TextBox(); tbUserName.ID = "tbUserName"; string strPatternUserName = @"\W"; // i've tried using "\\W", "\w", "\\w","@\w", "/\\w", etc... // but it always prints my error msg UNLESS the textbox is set to "". // why does this not work?
7
1908
by: Razzie | last post by:
Hey all, Decided to give a shot at Regular expressions - need a bit of help :) I can't seem to find the right regex for matching words like "*test*" or *somevalue*" - in short, all words starting and ending with a *. I tried things like string regex = @"(^*\|*^)"); but it still doesn't work completely (matches on "*test" too for example). If anything could help me with this, that would be appreciated.
18
3030
by: Q. John Chen | last post by:
I have Vidation Controls First One: Simple exluce certain special characters: say no a or b or c in the string: * Second One: I required date be entered in "MM/DD/YYYY" format: //+4 How ??
0
1620
by: Tom Shelton | last post by:
This is a simple example of a numeric only text box. It allows only the entry of integer values. The advantage of this example is that it also handles user attempts to past non integer text data into the text box... Option Strict On Option Explicit On Imports System Imports System.Windows.Forms Imports System.ComponentModel
2
3838
by: Thomas T. Veldhouse | last post by:
Hello. I have been working with a client of mine on their seriazation code and we seem to have run into an issue when we migrated from .NET 1.1 to .NET 2.0. We have a framework of business objects that are serialized for persistant storage. The object graph DOES change as development continues, but it is required that we be able to deserialized existing classes and handle it properly (version tolerance). With version 1.1 of the .NET...
5
7186
by: FBergemann | last post by:
I use SunOS 5.8, gcc 3.3.2, boost 1.33.1. I have build the entire boost package and try to compile a simple example: #include <iostream> #include <string> #include <boost/regex.hpp // Boost.Regex lib using namespace std;
1
2149
by: Alan Patterson | last post by:
I have installed boost 1.33.1 and everything compiled and installed fine on Windows using Visual C++ 8.0. I wrote the following simple code to test the regex library and it doesn't seem to work. In the following "Matched" is not output to cout. Am I missing something or do I have a problem with my regex install? #include <string>
4
1502
by: bg_ie | last post by:
I have the following Regex - oldString = @"C:\ta"; Regex regexEndsInBackslash = new Regex(oldString + @"\\", RegexOptions.Singleline | RegexOptions.IgnoreCase); Any ideas why none of the following are true - if (regexEndsInBackslash.Match(@"C:\ta\").Success == true) ....
0
8706
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
8944
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8899
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7786
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6550
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4391
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4638
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3075
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2364
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.