Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Master Coder
    Join Date
    Apr 2003
    Location
    in my house
    Posts
    5,211
    Thanks
    39
    Thanked 201 Times in 197 Posts

    regex not doing as I expect.

    Hi,

    I want to check a number being submitted to make sure that it comprises either 1 or two digits. ideally I'd like to make sure it is between 1 and 35.

    what I have done should (I thought) have made sure that the number submitted would have one or two digits. but it errors on 10. it accepts 8.

    Code:
    unless ( $value =~ /^[\d{1,2}]$/ ) { exit; }
    what have I done wrongly.

    bazz
    "The day you stop learning is the day you become obsolete"! - my late Dad.

    Why do some people say "I don't know for sure"? If they don't know for sure then, they don't know!
    Useful MySQL resource
    Useful MySQL link

  • #2
    Master Coder
    Join Date
    Apr 2003
    Location
    in my house
    Posts
    5,211
    Thanks
    39
    Thanked 201 Times in 197 Posts
    got it !!

    Code:
      unless ( $value =~ /^\d{1,2}$/ ) { exit; }
    bazz
    "The day you stop learning is the day you become obsolete"! - my late Dad.

    Why do some people say "I don't know for sure"? If they don't know for sure then, they don't know!
    Useful MySQL resource
    Useful MySQL link

  • #3
    Senior Coder
    Join Date
    Mar 2006
    Posts
    1,274
    Thanks
    2
    Thanked 39 Times in 38 Posts
    Quote Originally Posted by bazz View Post
    got it !!

    Code:
      unless ( $value =~ /^\d{1,2}$/ ) { exit; }
    bazz
    Few things have the same meta meaning inside a character class as they do outside a character class. The list of special characters inside of a character class is very short:

    -]\^$

    And some of those only have special meaning if they are used at a specific location inside the character class.

  • #4
    Master Coder
    Join Date
    Apr 2003
    Location
    in my house
    Posts
    5,211
    Thanks
    39
    Thanked 201 Times in 197 Posts
    Thanks Kevin.

    I know some of the effects of those inside a character class but not the others:

    ^ means not in
    - hyphen must come first
    \ escapes the next character


    what do the ] and $ do when positioned within a character class?

    bazz
    "The day you stop learning is the day you become obsolete"! - my late Dad.

    Why do some people say "I don't know for sure"? If they don't know for sure then, they don't know!
    Useful MySQL resource
    Useful MySQL link

  • #5
    Senior Coder
    Join Date
    Mar 2006
    Posts
    1,274
    Thanks
    2
    Thanked 39 Times in 38 Posts
    The '$' symbol is the same as outside, its a scalar datatype symbol.

    The ']' symbol is the end of a character class symbol, so in some usages inside a character class it will need to be escaped so its not interprted as the end of the character class.

    The '-' is the same as outside a character class, its the range operator, as in a-zA-Z0-9. So you have to escape it if its usage inside a character class can be interpreted as the range operator: [a\-z] but if you write it like this there is no need to escape it: [az-] because the context is clear: its not a range.

    The '^' symbol is the negated character class symbol if it is the very first character in a character class, otherwise its just a literal ^ symbol.

    The other special characters are whatever you use as the regexp delimiters, so if you used # you need to escape it:

    Code:
    if (m#[@!\#]#) {...}


  •  

    Posting Permissions

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