a simple one for the Regex-perts =)

K

K. Shier

my regex for matching phone #'s:
\({0,1}(?<areacode>[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!
 
J

Jeremy Cowles

my regex for matching phone #'s:
\({0,1}(?<areacode>[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
 
B

Brian Davis

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}(?<areacode>[0-9]{3})\){0,1}( |-){0,1}(?<prefix>[0-9]{3})(
|-){0,1}(?<suffix>[0-9]{4}).*


Brian Davis
www.knowdotnet.com
 
K

K. Shier

Thanks, all, for your responses! I knew the answer would be something
simultaneously simple and brilliant...

Brian Davis said:
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}(?<areacode>[0-9]{3})\){0,1}( |-){0,1}(?<prefix>[0-9]{3})(
|-){0,1}(?<suffix>[0-9]{4}).*


Brian Davis
www.knowdotnet.com



K. Shier said:
my regex for matching phone #'s:
\({0,1}(?<areacode>[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!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top