Simple Regex

Discussion in 'ASP .Net' started by shapper, Jun 12, 2007.

  1. shapper

    shapper Guest

    Hello,

    I need a regex expression that allows only numbers in the following
    format:

    2312-986

    The first number can't start with 0:
    0231-123 is invalid.

    However:
    1231-012 is valid

    Could someone help me out?

    Thanks,
    Miguel
     
    shapper, Jun 12, 2007
    #1
    1. Advertising

  2. [1-9]\d{3}-\d{3}

    Ought to do it, I would think. It checks out in my RegEx checker, anyway,
    using the examples you give.


    Peter


    "shapper" <> wrote in message
    news:...
    > Hello,
    >
    > I need a regex expression that allows only numbers in the following
    > format:
    >
    > 2312-986
    >
    > The first number can't start with 0:
    > 0231-123 is invalid.
    >
    > However:
    > 1231-012 is valid
    >
    > Could someone help me out?
    >
    > Thanks,
    > Miguel
    >
     
    Peter Bradley, Jun 12, 2007
    #2
    1. Advertising

  3. shapper

    Mark Rae Guest

    "shapper" <> wrote in message
    news:...

    > Could someone help me out?


    [1-9][0-9][0-9][0-9]-[0-9][0-9][0-9]

    I'm sure there are much shorter and cleverer ways using groups etc...


    --
    http://www.markrae.net
     
    Mark Rae, Jun 12, 2007
    #3
  4. Sorry to reply to myself, but I miscopied my RegEx. It should, of course,
    be:

    ^[1-9]\d{3}-\d{3}$

    Since I'm assuming you want simply this format and nothing else. If you
    leave the start and finish markers (^, $) out then it will match the format
    anywhere in the expression.
    e.g. 2222abd1234-023xxxx would match

    Apologies


    Peter

    "Peter Bradley" <> wrote in message
    news:...
    > [1-9]\d{3}-\d{3}
    >
    > Ought to do it, I would think. It checks out in my RegEx checker, anyway,
    > using the examples you give.
    >
    >
    > Peter
    >
    >
    > "shapper" <> wrote in message
    > news:...
    >> Hello,
    >>
    >> I need a regex expression that allows only numbers in the following
    >> format:
    >>
    >> 2312-986
    >>
    >> The first number can't start with 0:
    >> 0231-123 is invalid.
    >>
    >> However:
    >> 1231-012 is valid
    >>
    >> Could someone help me out?
    >>
    >> Thanks,
    >> Miguel
    >>

    >
    >
     
    Peter Bradley, Jun 12, 2007
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?SmViQnVzaGVsbA==?=

    Is ASP Validator Regex Engine Same As VS2003 Find Regex Engine?

    =?Utf-8?B?SmViQnVzaGVsbA==?=, Oct 22, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    713
    =?Utf-8?B?SmViQnVzaGVsbA==?=
    Oct 22, 2005
  2. Rick Venter

    perl regex to java regex

    Rick Venter, Oct 29, 2003, in forum: Java
    Replies:
    5
    Views:
    1,635
    Ant...
    Nov 6, 2003
  3. Replies:
    2
    Views:
    601
  4. Xah Lee
    Replies:
    1
    Views:
    945
    Ilias Lazaridis
    Sep 22, 2006
  5. Replies:
    3
    Views:
    772
    Reedick, Andrew
    Jul 1, 2008
Loading...

Share This Page