Forum Discussion

Robert_47833's avatar
Robert_47833
Icon for Altostratus rankAltostratus
Nov 22, 2011

how to match 6 digtals via command except matches_regex

hi,dear irule

 

if {$uri matches_regex {-[0-9][0-9][0-9][0-9][0-9][0-9]}} {

 

pool xxx

 

}

 

I don't wanna use matches_regex any more

 

do u have an alternation method to achieve this?

 

thanks in advance.

 

  • Sorry, I was mistakenly thinking scan would fail to match at all if the full string wasn't matched. You can use string is digit for this:

     

     

    % string is digit 100000000000

     

    1

     

    % string is digit 100000000000000000000000000000

     

    1

     

    % string is digit 100abc

     

    0

     

     

    Aaron
  • So this should work for your scenario to check for a directory of all digits:

    
    if { [string is digit [getfield [HTTP::uri] "/" 2]] } {
       log local0. " [getfield [HTTP::uri] "/" 2] is ok"
    } else {
       log local0. " [getfield [HTTP::uri] "/" 2] is not ok"
    }
    

    Aaron
  • -------------

     

    from Joel:

     

     

    "string is digit" can include characters outside of the standard [0-9] in unicode if they are meant to represent numbers in other languages.

     

  • Yes, I wasn't too concerned about a URI containing non-ascii characters as HTTP URIs must contain only printable ASCII characters (and/or percent encodings of reserved characters). Here are a couple of references:

     

     

    http://en.wikipedia.org/wiki/Internationalized_domain_name

     

    http://stackoverflow.com/questions/2504564/can-http-uris-have-non-ascii-characters

     

     

    If you did need to match only 6 0-9 characters you could use [string is numeric $chars] as 'is numeric' returns true for 999999. It wouldn't work once you surpass the limit for an integer (I think this is possibly version and platform specific). Or you could use [string is double $chars] if you want to allow decimal values as well. Or if you want to be more explicit on your matching, you could go back to a regex like [0-9]{6} to match exactly six digits or [0-9]{1,6} to match 1 to 6 digits.

     

     

    Aaron