Regular Expression Help

Discussion in 'ASP .Net' started by tdavisjr, Jan 18, 2006.

  1. tdavisjr

    tdavisjr Guest

    Anyone can give me a quick RegEx pattern that would match a 7 or 10
    digit number. This is not a range; but exactly 7 or 10. I'm beating
    my head over this, nothing I am doing works. Thanks
     
    tdavisjr, Jan 18, 2006
    #1
    1. Advertisements

  2. I believe this works:

    ^\d{7}(\d{3})?$

    it makes the last 3 digits optional (as a group)

    Karl
     
    Karl Seguin [MVP], Jan 18, 2006
    #2
    1. Advertisements

  3. Hi,

    You will probably find RegExlib.com to be very helpful. It has a
    library of custome regular expressions. There is also a tool that
    allows you to test your own regular expressions.
     
    Tod Birdsall, MCSD for .NET, Jan 18, 2006
    #3
  4. tdavisjr

    tdavisjr Guest

    Thanks Karl. That seem to work. I probably need to adjust my thinking
    when doing RegEx's. I was trying to do something like this: (\d{7}) |
    (d{10}) when it was either or. I never thought about making the last
    part optional as a group.

    And Tod, Thanks for the reference. I did visit this site and used its
    tester, however, I searched the database and I could find a simple
    example for what I wanted.
     
    tdavisjr, Jan 18, 2006
    #4
  5. That works too, you just have your parenthesis in the wrong place:

    ^(\d{7}|\d{10})$

    Karl
     
    Karl Seguin [MVP], Jan 18, 2006
    #5
    1. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.