Reg expression

  • Thread starter Thread starter MSDN
  • Start date Start date
M

MSDN

Hello,

Does anyone what RegExp to use for the following.

3-digits followed by "-" or " " then Followed by 6 or 7 digits then followed
by any number of spaces.

111 111111
111-111111

222-123456"" space at the end okay

231 1345678

I just can not figure out the end spaces...

Thank you

SA
 
AlanT,

For some reason the end spaces are not excepted.
I am using this RegExp in a Reg Expression Validator
I had something similar but yours is much simpler.

Also on my Web page if I put 543-214 it says is okay????

I thought it must have 3 digits followed by an optional space or "-" then
followed by 6 digits.
What is going on
Is my Validation screwed up??

Thank you Alan

SA
 
Hi,

Alan's suggestion works splendidly. The only drawback I can see is the
use of back-references, which don't seem to be required in this case.
Also, Alan's Regex does not catch 543-214, so there seems to be an
issue with the Regex validator you're using.

Try testing your Regex at <http://regexlib.com/RETester.aspx>.

Here's my Regex for the same (doesn't use back-references) :

\d{3}[ -]\d{6}\d*\s*

Note : If your lines are going to start and end with the phone no., you
might prefix the Regex with ^ and suffix it with $. Then turn MultiLine
mode on. This would make it more stringent.

Hope this helps,

Regards,

Cerebrus.
 
They are not back references, just alternation groupings

(-| )

is just a grouping for '-' or ' '.

It does exactly the same as

[- ]

which is actually simpler. my bad.


the one comment on the above is that is will accept

111-1234345545454545454545

as the \d* will be satisfied by the 5545454545454545


Revised version

\d{3}[- ]\d{6}(\d|\s*)


Simpler construct for the internal space or hyphen choice
Uses \s instead of ' ' for the terminal check to allow for any type of
whitespace rather than just spaces

hth,
Alan.
 
Hi Alan,

Oops ! The \d* was a typo !!! And that is *one* thing totally
unacceptable in a Regex... My apologies ;-)

But I would still discourage the use of brackets (\d|\s*), unless only
a small amount of text is to be processed, or you use the
ExplicitCapture option.

Thanks and Regards,

Cerebrus.
 
Gentelmen,

Yes I had some issue with my web page and the expression was not evaluating
right
after restarting the process it works

I end up using
^\d{3}[-| ]?\d{6}\d?[ ]?$

I can have a 7th digit but not necessary I can have a space at the end

I tried [ ]* but for some reason it did not like it, I want zero or more
spaces at the end



Thank you all for your help



SA
 
Back
Top