RegularExpressions.Regex

  • Thread starter Thread starter pfeifest
  • Start date Start date
P

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
 
pfeifest said:
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".
 
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.
 
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
 
pfeifest said:
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. :)
 
Back
Top