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.
--------------------
From: "Brad Prendergast" <format('bradp%sbpsoftware.com',['@'])>
Subject: Re: More regular expression woes
References: <ud4wgxvQHHA.4744@xxxxxxxxxxxxxxxxxxxx>
User-Agent: XanaNews/1.18.1.5
Message-ID: <xn0f1qvzhcrzq1000@xxxxxxxxxxxxxxxxxxxx>
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!TK2MSFTNGP03.phx.gbl
Xref: TK2MSFTNGHUB02.phx.gbl microsoft.public.dotnet.languages.csharp:11378
X-Tomcat-NG: microsoft.public.dotnet.languages.csharp

Mark Rae <mark@xxxxxxxxxxxxxxxxx> wrote in message
<ud4wgxvQHHA.4744@xxxxxxxxxxxxxxxxxxxx>:

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@xxxxxxxxxxxxx\par
\f1\par
}

.



Relevant Pages

  • Re: Fastest way to search a string for the occurance of a word??
    ... but the OP's question was what's the "Fastest way to search a string ... in all the tests I did here, the Regex was by far superior. ... However, of course, if you've got new regular expressions all ... Sure - but just that extra Match object could be relevant if the search ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: regular expression help
    ... Basically because if you remove everything that is optional in the regex below you end up with an empty regex: ... So the regex engine will try to match on every character in the string: ... , comma doesn't match, but the nothingness in front of it does. ... A quote followed by any sequence of characters that is not a quote, ...
    (microsoft.public.dotnet.framework)
  • Re: Regex optimization
    ... I was hoping that someone with knowledge of the Regex engine could ... match per string for either Regex. ... reluctant modifier, may be slower .*?, +? ... Variable parts will try to capture as much as possible. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Regex question
    ... If I use the following string in C# for a validation control: ... regex tester programs such as Expresso. ... but it fails in my code.. ... And finally you could also use 2 validators, one to validate the length and a second to validate the pattern. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Regex Capture problem
    ... "learned" my regex using a freeware utility that had slightly different ... was trying to capture instead of. ... I have used Regex utilities before, so I understand the concepts of text ... Function RESub(str As String, SrchFor As String, ReplWith As String) As String ...
    (microsoft.public.excel.programming)