Connecting Tech Pros Worldwide Forums | Help | Site Map

More regular expression woes

Mark Rae
Guest
 
Posts: n/a
#1: Jan 28 '07
Hi,

This time, I'm looking for a regular expression which says "the string must
contain exactly seven or exactly eight digits" e.g.

123456 fails
1234567 passes
12345678 passes
123456789 fails

I've tried this:

\d{7,8}

but that allows 123456789 to pass, presumably because it contains a string
of seven or eight digits...

Is there any way to specifiy a fixed length to validate?

Any assistance gratefully received.

Mark


Oliver Sturm
Guest
 
Posts: n/a
#2: Jan 28 '07

re: More regular expression woes


Hello Mark,
Quote:
>I've tried this:
>
>\d{7,8}
>
>but that allows 123456789 to pass, presumably because it contains a string
>of seven or eight digits...
Right. To do what you want you'll have to use a delimiter surrounding the
expression. ^ (start of line) and $ (end of line) could do, like this:

^\d{7,8}$

Of course, if you have strings that contain nothing but that number you're
looking at, it may be considerably more performant to look at the length
of the string combined with a simpler check that the characters are all
digits (leaving out regular expressions entirely).

Another delimiter that could be useful to you would be \b, which denotes a
"word boundary". For the exact definition you'd best look at the regular
expression docs on MSDN.

Yet another idea could be to delimit by characters that are not digits -
this makes sense and can be very helpful in more complex situations. Like
this:

[^\d]\d{7,8}[^\d]



Oliver Sturm
--
http://www.sturmnet.org/blog
Brad Prendergast
Guest
 
Posts: n/a
#3: Jan 28 '07

re: More regular expression woes


Mark Rae <mark@markNOSPAMrae.comwrote in message
<ud4wgxvQHHA.4744@TK2MSFTNGP02.phx.gbl>:
Quote:
>Hi,
>
>This time, I'm looking for a regular expression which says "the
>string must contain exactly seven or exactly eight digits" e.g.
>
>123456 fails
>1234567 passes
>12345678 passes
>123456789 fails
>
>I've tried this:
>
>\d{7,8}
>
>but that allows 123456789 to pass, presumably because it contains a
>string of seven or eight digits...
>
>Is there any way to specifiy a fixed length to validate?
>
>Any assistance gratefully received.
>
>Mark
Regex r;
r = new Regex("^\\d{7,8}$");

--
Brad Prendergast
"There's a fine line between genius and insanity. I have erased this
line." -- Oscar Levant (1906 - 1972)
Mark Rae
Guest
 
Posts: n/a
#4: Jan 28 '07

re: More regular expression woes


"Oliver Sturm" <oliver@sturmnet.orgwrote in message
news:xn0f1r3lz3u9cac00x@msnews.microsoft.com...
Quote:
Right. To do what you want you'll have to use a delimiter surrounding the
expression. ^ (start of line) and $ (end of line) could do, like this:
>
^\d{7,8}$
Supoib!
Quote:
Of course, if you have strings that contain nothing but that number you're
looking at, it may be considerably more performant to look at the length
of the string combined with a simpler check that the characters are all
digits (leaving out regular expressions entirely).
Of course you're right about that. I'm finally (and I mean after nearly 20
years of programming!) trying to actually learn regular expressions
properly...

I'm using this: http://www.ultrapico.com/Expresso.htm which seems about the
best I've seen so far...
Quote:
Another delimiter that could be useful to you would be \b, which denotes a
"word boundary". For the exact definition you'd best look at the regular
expression docs on MSDN.
Cool - thanks.
Quote:
Yet another idea could be to delimit by characters that are not digits -
this makes sense and can be very helpful in more complex situations. Like
this:
>
[^\d]\d{7,8}[^\d]
Excellent - thanks again.


Oliver Sturm
Guest
 
Posts: n/a
#5: Jan 28 '07

re: More regular expression woes


Hello Mark,
Quote:
>Of course you're right about that. I'm finally (and I mean after nearly 20
>years of programming!) trying to actually learn regular expressions
>properly...
A very good idea, if you ask me :-) Of course, learning the right
situations where to use them is just as important :-) I think the context
of your use case is probably very important to make that decision.
Quote:
>I'm using this: http://www.ultrapico.com/Expresso.htm which seems about
>the best I've seen so far...
There's also Regulator (http://sourceforge.net/projects/regulator/) which
is very good (better than Expresso IMO), but I've had pretty bad problems
in the past with the editor component used in that program and the author
Roy Osherove hasn't been responsive at all when I sent him bug reports.


Oliver Sturm
--
http://www.sturmnet.org/blog
Laurent Bugnion [MVP]
Guest
 
Posts: n/a
#6: Jan 28 '07

re: More regular expression woes


Hi,

Oliver Sturm wrote:
Quote:
Hello Mark,
>
Quote:
>Of course you're right about that. I'm finally (and I mean after
>nearly 20 years of programming!) trying to actually learn regular
>expressions properly...
>
A very good idea, if you ask me :-) Of course, learning the right
situations where to use them is just as important :-) I think the
context of your use case is probably very important to make that decision.
>
Quote:
>I'm using this: http://www.ultrapico.com/Expresso.htm which seems
>about the best I've seen so far...
>
There's also Regulator (http://sourceforge.net/projects/regulator/)
which is very good (better than Expresso IMO), but I've had pretty bad
problems in the past with the editor component used in that program and
the author Roy Osherove hasn't been responsive at all when I sent him
bug reports.
>
>
Oliver Sturm
I like Regulator too. Didn't have issues so far (but I don't use it very
intensively).

Greetings,
Laurent
--
Laurent Bugnion [MVP ASP.NET]
Software engineering: http://www.galasoft-LB.ch
PhotoAlbum: http://www.galasoft-LB.ch/pictures
Support children in Calcutta: http://www.calcutta-espoir.ch
Oliver Sturm
Guest
 
Posts: n/a
#7: Jan 28 '07

re: More regular expression woes


Hello Laurent,
Quote:
>I like Regulator too. Didn't have issues so far (but I don't use it very
>intensively).
The issues I mean are related to two things: (a) Changing regex options
while there's already an expression in the editor. This often makes sudden
changes to the expression for no apparent reason. (b) Cutting and pasting
text from/to the editor window. This makes changes to the text in places
that shouldn't be affected by the operation.

Not sure if there were more... I haven't used it recently and it's been a
while since I tried reporting the problems I saw. If somebody's
interested, I can dig up my original email(s?) and see what other
information I might have.


Oliver Sturm
--
http://www.sturmnet.org/blog
John Kn [MS]
Guest
 
Posts: n/a
#8: Jan 30 '07

re: More regular expression woes


Brad,

Try this:
public static Regex regex = new Regex(
@"\b\d{7}\b|\b\d{8}\b",
RegexOptions.IgnoreCase
| RegexOptions.CultureInvariant
| RegexOptions.IgnorePatternWhitespace
| RegexOptions.Compiled
);

Do not let the \b expressions throw you. Additionally, you may want to
download Expresso from http://www.ultrapico.com/. It is the best regex
tutorial/utility I have found over the years.
--------------------
Quote:
>From: "Brad Prendergast" <format('bradp%sbpsoftware.com',['@'])>
>Subject: Re: More regular expression woes
>References: <ud4wgxvQHHA.4744@TK2MSFTNGP02.phx.gbl>
>User-Agent: XanaNews/1.18.1.5
>Message-ID: <xn0f1qvzhcrzq1000@msnews.microsoft.com>
>X-Ref: msnews.microsoft.com ~XNS:00000018
>Newsgroups: microsoft.public.dotnet.languages.csharp
>Date: Sun, 28 Jan 2007 09:11:35 -0800
>NNTP-Posting-Host: c-76-19-190-126.hsd1.ma.comcast.net 76.19.190.126
>Lines: 1
>Path: TK2MSFTNGHUB02.phx.gbl!TK2MSFTNGP01.phx.gbl!TK2MSF TNGP03.phx.gbl
>Xref: TK2MSFTNGHUB02.phx.gbl microsoft.public.dotnet.languages.csharp:11378
>X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
>
>Mark Rae <mark@markNOSPAMrae.comwrote in message
><ud4wgxvQHHA.4744@TK2MSFTNGP02.phx.gbl>:
>
Quote:
>>Hi,
>>
>>This time, I'm looking for a regular expression which says "the
>>string must contain exactly seven or exactly eight digits" e.g.
>>
>>123456 fails
>>1234567 passes
>>12345678 passes
>>123456789 fails
>>
>>I've tried this:
>>
>>\d{7,8}
>>
>>but that allows 123456789 to pass, presumably because it contains a
>>string of seven or eight digits...
>>
>>Is there any way to specifiy a fixed length to validate?
>>
>>Any assistance gratefully received.
>>
>>Mark
>
Regex r;
r = new Regex("^\\d{7,8}$");
>
>--
>Brad Prendergast
>"There's a fine line between genius and insanity. I have erased this
>line." -- Oscar Levant (1906 - 1972)
>
{\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl {\f0\fnil\fprq2\fcharset0
MS Sans Serif;}{\f1\fswiss\fcharset0 Arial;}}
{\*\generator Msftedit 5.41.21.2500;}\viewkind4\uc1\pard\f0\fs20 Cheers,\par
\par
johnKn [MS-SDK]\par
\par
\par
\par
-Please do not send email directly to this alias. This alias is for \par
newsgroup purposes only\par
\par
-This posting is provided "AS IS" with no warranties, and confers no
rights.\par
\par
-To provide additional feedback about your community experience please send
\par
e-mail to: sdkcomm@microsoft.com\par
\f1\par
}

Oliver Sturm
Guest
 
Posts: n/a
#9: Jan 30 '07

re: More regular expression woes


Hello John,
Quote:
@"\b\d{7}\b|\b\d{8}\b",
An interesting additional idea, although obviously not really flexible - I
would opt for the flexibility of \d{7,8} as long as I can make it work.
Furthermore, your example could be less confusing, IMO, as

\b(\d{7}|\d{8})\b

Or of course, as

\b\d{7,8}\b

which brings us back to what has already been mentioned. Maybe I'm missing
the point of your post?


Oliver Sturm
--
http://www.sturmnet.org/blog
John Kn [MS]
Guest
 
Posts: n/a
#10: Jan 30 '07

re: More regular expression woes


Oliver,

\b\d{7,8}\b works for me.

Thanks,

John
--------------------
Quote:
>From: "Oliver Sturm" <oliver@sturmnet.org>
>Subject: Re: More regular expression woes
>References: <ud4wgxvQHHA.4744@TK2MSFTNGP02.phx.gbl>
<xn0f1qvzhcrzq1000@msnews.microsoft.com>
<ay7lzjJRHHA.196@TK2MSFTNGHUB02.phx.gbl>
Quote:
>Date: Tue, 30 Jan 2007 19:39:36 +0000
>User-Agent: XanaNews/1.18.1.3
>Message-ID: <xn0f1u0vm1irnlh00q@msnews.microsoft.com>
>X-Ref: msnews.microsoft.com ~XNS:00000864
>MIME-Version: 1.0
>Content-Type: text/plain; format=flowed
>Newsgroups: microsoft.public.dotnet.languages.csharp
>NNTP-Posting-Host: 83-216-141-166.oliver856.adsl.metronet.co.uk
83.216.141.166
Quote:
>Lines: 1
>Path: TK2MSFTNGHUB02.phx.gbl!TK2MSFTNGP01.phx.gbl!TK2MSF TNGP04.phx.gbl
>Xref: TK2MSFTNGHUB02.phx.gbl microsoft.public.dotnet.languages.csharp:11780
>X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
>
>Hello John,
>
Quote:
> @"\b\d{7}\b|\b\d{8}\b",
>
>An interesting additional idea, although obviously not really flexible - I
>would opt for the flexibility of \d{7,8} as long as I can make it work.
>Furthermore, your example could be less confusing, IMO, as
>
\b(\d{7}|\d{8})\b
>
>Or of course, as
>
\b\d{7,8}\b
>
>which brings us back to what has already been mentioned. Maybe I'm missing
>the point of your post?
>
>
Oliver Sturm
>--
>http://www.sturmnet.org/blog
>
{\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl {\f0\fnil\fprq2\fcharset0
MS Sans Serif;}{\f1\fswiss\fcharset0 Arial;}}
{\*\generator Msftedit 5.41.21.2500;}\viewkind4\uc1\pard\f0\fs20 Cheers,\par
\par
johnKn [MS-SDK]\par
\par
\par
\par
-Please do not send email directly to this alias. This alias is for \par
newsgroup purposes only\par
\par
-This posting is provided "AS IS" with no warranties, and confers no
rights.\par
\par
-To provide additional feedback about your community experience please send
\par
e-mail to: sdkcomm@microsoft.com\par
\f1\par
}

Closed Thread


Similar C# / C Sharp bytes