Forum Discussion

SSHSSH_97332's avatar
SSHSSH_97332
Icon for Nimbostratus rankNimbostratus
May 14, 2012

Regular Expression to Force Parameter to be in IP Format

i need to force parameter value to be in IP format : ( 3 digits . 3 digits . 3 digits . 3 digits ) , i found option called regex ( regular expression ) under parameters Tab . How can i define regex to do my target ?

 

 

5 Replies

  • nathe's avatar
    nathe
    Icon for Cirrocumulus rankCirrocumulus
    SSHSSH

     

     

    ([0-9]{3,}\.){3,3}[0-9]{3,}

     

     

    By putting in this regex expression it will look for a parameter in the format xxx.xxx.xxx.xxx (but not xx.xx.xx.xx or similar) Is that what you're after?

     

     

    Also, F5 has a neat regex validator tool (App Security, Options, Tools, RegExp Validator) where you can pop this string in and enter a test string e.g. 111.111.111.111 as an example, and it will tell you whether there's a match or not.

     

     

    Hope this helps,

     

    N
  • Every tool has a slightly different implementation of regex. I haven't tested this on F5 yet, so it might not be quite right, but give it a try. I use it ALOT on other systems.

     

     

    [0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}

     

     

     

     

     

     

    If f5 supports perl regexes, you can simplify it with

     

    \d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}

     

     

     

    The important difference between this and the previous poster's regex is it allows each octet in the ip (the octet is the 0-255 number) to be 1-3 digits. Most systems don't 0-pad the octets into 3 digit numbers, so you need this range.

     

     

     

    This regex isn't perfect. It does not force the numbers to be 0-255 ( would match) but normally that is not a problem. However, you might need a more precise solution, for example, if you're trying to protect a parameter that lacks data validation. It probably is possible to write such a regex (I would try look-aheads), but it would be very complex and difficult for others to understand. I would advise against a regex in such a case.

     

  • Here is an example of a regex that should work to force the numbers to be between 0 and 255. This is just one octet - you would have to do this 4 times, with \. between each.

     

     

    (25[0-5])|(2[0-4][0-9])|([0-1][0-9][0-9])|([0-9]{1,2})

     

     

    So the full regex would be:

     

     

    (25[0-5])|(2[0-4][0-9])|([0-1][0-9][0-9])|([0-9]{1,2})\.(25[0-5])|(2[0-4][0-9])|([0-1][0-9][0-9])|([0-9]{1,2})\.(25[0-5])|(2[0-4][0-9])|([0-1][0-9][0-9])|([0-9]{1,2})\.(25[0-5])|(2[0-4][0-9])|([0-1][0-9][0-9])|([0-9]{1,2})

     

     

    Yuck.
  • Oops. Missed some parenthesis...

     

     

    ((25[0-5])|(2[0-4][0-9])|([0-1][0-9][0-9])|([0-9]{1,2}))\.((25[0-5])|(2[0-4][0-9])|([0-1][0-9][0-9])|([0-9]{1,2}))\.((25[0-5])|(2[0-4][0-9])|([0-1][0-9][0-9])|([0-9]{1,2}))\.((25[0-5])|(2[0-4][0-9])|([0-1][0-9][0-9])|([0-9]{1,2}))

     

     

    Definitely yuck. :)
  • Thanks All . where i can find guide & examples fro regular expressions ?