|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
RegularExpressions.RegexHello-
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 <pfeif***@hotmail.com> wrote:
> I have a number that always starts with the three characters "450" and I am unable to reproduce the problem. I tested with the following:> 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. 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.
-- Show quoteHTH, Kevin Spencer Microsoft MVP ..Net Developer We got a sick zebra a hat, you ultimate tuna. "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". 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 <pfeif***@hotmail.com> wrote:
> Ugh... my bad. I was evaluating it wrong. In that case, IsMatch returns true because the first 10 digits are matched> > This number still comes back as valid though - even though it is 11 > long instead of the required 10: > > 45025771762 (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. :) |
|||||||||||||||||||||||