Home All Groups Group Topic Archive Search About

RegularExpressions.Regex

Author
15 Feb 2006 5:47 PM
pfeifest
Hello-

I have a number that always starts with the three characters "450" and
then has 7 random numeric digits after it.

Example:  4507432176

What Regular expression will validate that scenario?  I've tried
various combinations of:

450\d{7}

But basically any 10-digit number comes back as valid with that.

Thanks-

pfeifest

Author
15 Feb 2006 7:54 PM
tjb
pfeifest <pfeif***@hotmail.com> wrote:

> I have a number that always starts with the three characters "450" and
> then has 7 random numeric digits after it.
>
> Example:  4507432176
>
> What Regular expression will validate that scenario?  I've tried
> various combinations of:
>
> 450\d{7}
>
> But basically any 10-digit number comes back as valid with that.

I am unable to reproduce the problem. I tested with the following:

    using System;
    using System.Text.RegularExpressions;

    class Test
    {
        public static void Main() {
            Regex r = new Regex(@"450\d{7}");
            Console.WriteLine(r.IsMatch("4509999999"));
            Console.WriteLine(r.IsMatch("4519999999"));
        }
    }

This works as expected, printing "True" and then "False".
Author
15 Feb 2006 8:24 PM
Kevin Spencer
Yup. Works for me too. As it should.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
We got a sick zebra a hat,
you ultimate tuna.


Show quote
"tjb" <tjb@invalid.invalid> wrote in message
news:1x5jvrxtes81j$.dlg@tjb.invalid.invalid...
> pfeifest <pfeif***@hotmail.com> wrote:
>
>> I have a number that always starts with the three characters "450" and
>> then has 7 random numeric digits after it.
>>
>> Example:  4507432176
>>
>> What Regular expression will validate that scenario?  I've tried
>> various combinations of:
>>
>> 450\d{7}
>>
>> But basically any 10-digit number comes back as valid with that.
>
> I am unable to reproduce the problem. I tested with the following:
>
>    using System;
>    using System.Text.RegularExpressions;
>
>    class Test
>    {
>        public static void Main() {
>            Regex r = new Regex(@"450\d{7}");
>            Console.WriteLine(r.IsMatch("4509999999"));
>            Console.WriteLine(r.IsMatch("4519999999"));
>        }
>    }
>
> This works as expected, printing "True" and then "False".
Author
15 Feb 2006 8:29 PM
pfeifest
Ugh... my bad.  I was evaluating it wrong.

This number still comes back as valid though - even though it is 11
long instead of the required 10:

45025771762
Author
15 Feb 2006 8:59 PM
tjb
pfeifest <pfeif***@hotmail.com> wrote:

> Ugh... my bad.  I was evaluating it wrong.
>
> This number still comes back as valid though - even though it is 11
> long instead of the required 10:
>
> 45025771762

In that case, IsMatch returns true because the first 10 digits are matched
(all but '2' at the end): even though it didn't match *all* of the
characters, it found a match nonetheless. Matching ten digits "on their
own" involves using anchors.

The original regex was: 450\d{7}

Using start-of-string and end-of-string anchors makes that: ^450\d{7}$

These are described in <http://www.regular-expressions.info/anchors.html>.
If anything on that page doesn't make sense you'd probably be best off
reading the entire tutorial. :)

AddThis Social Bookmark Button