Results 1 to 11 of 11

Thread: [2.0] regex question

  1. #1

    Thread Starter
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083

    Resolved [2.0] regex question

    How would you go about doing this using regex :

    I want to validate "Age" field which complies to these rules:
    1.doesn't accept "zero" at the beginning.
    2.doesn't excced 3 digits.
    3.accept 2 or 3 digits.

    examples of valid ages: 20 , 38,102

    Sorry , can't construct this little regex string

    Thank u for any help !
    Last edited by Pirate; Dec 18th, 2007 at 10:33 AM.

  2. #2
    Code Monkey wild_bill's Avatar
    Join Date
    Mar 2005
    Location
    Montana
    Posts
    2,993

    Re: [2.0] regex question

    Try this
    Code:
    ^[1-9][0-9]{0,2}$

  3. #3

    Thread Starter
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083

    Re: [2.0] regex question

    Quote Originally Posted by wild_bill
    Try this
    Code:
    ^[1-9][0-9]{0,2}$
    I tested in Expresso,returned no results!so it's not working

  4. #4
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704

    Re: [2.0] regex question

    WildBill's regex would work if you enclose it in paranthesis:
    ^([1-9][0-9]{0,2})$

    However, it also creates a grouping on the last digit.

    Try this:
    ^([1-9][0-9][0-9]*)*

  5. #5

    Thread Starter
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083

    Re: [2.0] regex question

    Quote Originally Posted by nemaroller
    WildBill's regex would work if you enclose it in paranthesis:
    ^([1-9][0-9]{0,2})$

    However, it also creates a grouping on the last digit.

    Try this:
    ^([1-9][0-9][0-9]*)*
    same result..no matching

  6. #6
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704

    Re: [2.0] regex question

    How do you mean no matches?

    I get many matches.

    Try the same regex in Regex lib:

    http://regexlib.com/RETester.aspx

  7. #7
    Code Monkey wild_bill's Avatar
    Join Date
    Mar 2005
    Location
    Montana
    Posts
    2,993

    Re: [2.0] regex question

    I tested it with vb.net, and it worked.
    Code:
    Dim rx As New Regex("^[1-9][0-9]{0,2}$")
    Console.WriteLine(rx.IsMatch("20"))
    Console.WriteLine(rx.IsMatch("38"))
    Console.WriteLine(rx.IsMatch("102"))
    Console.WriteLine(rx.IsMatch("3"))
    Console.WriteLine(rx.IsMatch("1022"))
    Console.WriteLine(rx.IsMatch("03"))

  8. #8

    Thread Starter
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083

    Re: [2.0] regex question

    Sorry,multiline was disabled in Expresso..all regex are working

    Thanks all

  9. #9
    Code Monkey wild_bill's Avatar
    Join Date
    Mar 2005
    Location
    Montana
    Posts
    2,993

    Re: [2.0] regex question

    You might consider modifying so no values > 199 are allowed.

  10. #10
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704

    Re: [2.0] regex question

    He could have meant age as in the number of days of an outstanding invoice.

    He never clarified that though.

  11. #11

    Thread Starter
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    I will consider that next time

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width