473,396 Members | 1,810 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

More regular expression woes

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
Jan 28 '07 #1
9 1529
Hello Mark,
>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
Jan 28 '07 #2
Mark Rae <ma**@markNOSPAMrae.comwrote in message
<ud**************@TK2MSFTNGP02.phx.gbl>:
>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)
Jan 28 '07 #3
"Oliver Sturm" <ol****@sturmnet.orgwrote in message
news:xn****************@msnews.microsoft.com...
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!
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...
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.
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.
Jan 28 '07 #4
Hello Mark,
>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.
>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
Jan 28 '07 #5
Hi,

Oliver Sturm wrote:
Hello Mark,
>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.
>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
Jan 28 '07 #6
Hello Laurent,
>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
Jan 28 '07 #7
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.
--------------------
>From: "Brad Prendergast" <format('bradp%sbpsoftware.com',['@'])>
Subject: Re: More regular expression woes
References: <ud**************@TK2MSFTNGP02.phx.gbl>
User-Agent: XanaNews/1.18.1.5
Message-ID: <xn***************@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 <ma**@markNOSPAMrae.comwrote in message
<ud**************@TK2MSFTNGP02.phx.gbl>:
>>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: sd*****@microsoft.com\par
\f1\par
}

Jan 30 '07 #8
Hello John,
@"\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
Jan 30 '07 #9
Oliver,

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

Thanks,

John
--------------------
>From: "Oliver Sturm" <ol****@sturmnet.org>
Subject: Re: More regular expression woes
References: <ud**************@TK2MSFTNGP02.phx.gbl>
<xn***************@msnews.microsoft.com>
<ay*************@TK2MSFTNGHUB02.phx.gbl>
>Date: Tue, 30 Jan 2007 19:39:36 +0000
User-Agent: XanaNews/1.18.1.3
Message-ID: <xn****************@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
>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,
> @"\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: sd*****@microsoft.com\par
\f1\par
}

Jan 30 '07 #10

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

Similar topics

23
by: Mark (News) | last post by:
I'm not really sure where to post this question as it covers so many platforms, but as the platform isn't relevant, here goes... I'm trying to (pulling my hair out more like) construct a regular...
4
by: Buddy | last post by:
Can someone please show me how to create a regular expression to do the following My text is set to MyColumn{1, 100} Test I want a regular expression that sets the text to the following...
11
by: Dimitris Georgakopuolos | last post by:
Hello, I have a text file that I load up to a string. The text includes certain expression like {firstName} or {userName} that I want to match and then replace with a new expression. However,...
0
by: Jim Mace | last post by:
I have been racking my brain trying to figure out a regular expression that will replace all of the characters in a string except the very first character from uppercase to lower case. Any ideas. I...
7
by: Billa | last post by:
Hi, I am replaceing a big string using different regular expressions (see some example at the end of the message). The problem is whenever I apply a "replace" it makes a new copy of string and I...
9
by: Trev | last post by:
Hi everyone, I'm having some problems with my regular expressions in C# and was wondering if anyone could help? Basically, what I'm trying to do is check for numerical only input - either...
35
by: Bjoern Hoehrmann | last post by:
Hi, For a free software project, I had to write a routine that, given a Unicode scalar value U+0000 - U+10FFFF, returns an integer that holds the UTF-8 encoded form of it, for example, U+00F6...
0
by: altavim | last post by:
Usually when you make regular expression to extract text you are starting from simple expression. When you got to know target text, you are extending your expression. Subsequently very hard to ready...
1
by: NvrBst | last post by:
I want to use the .replace() method with the regular expression /^ %VAR % =,($|&)/. The following DOESN'T replace the "^default.aspx=,($|&)" regular expression with "":...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...

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.