paolo@bimodesign.com | +34 608 61 64 10

Unclassifiable

        

Regexp - An explication

Starting to check using the RegExp, the latitude and longitude using these examples

49.082243,19.302628  

48.234142,19.200423  

49.002524,19.312578

where longitude goes from -180 to 180 and latitude goes from -90 to 90, assuming the order is "latitude, longitude". The possible solution could be this

/^(-?[1-8]?\d(?:\.\d{1,6})?|90(?:\.0{1,6})?),(-?(?:1[0-7]|[1-9])?\d(?:\.\d{1,6})?|180(?:\.0{1,6})?)$/
where
( Match the regular expression below and capture its match into backreference number 1
- Match the character “-” literally
? Between zero and one times, as many times as possible, giving back as needed (greedy)
\d{1,6} requires 1 or 6 decimal digits
\. requires a decimal point
, matches a single comma
$ anchors at the end of input

This web resource provides a complete list and description of the special characters that can be used in regular expressions.

Finally, this is a little list of usefull regexp to check some strings.
To avoid script injection

/((\%3C)|(\<)|<)(script\b)[^>]*((\%3E)|(\>)|>)(.*?)((\%3C)|(\<)|<)(\/script)((\%3E)|(\>)|>)|((\%3C)|<)((\%69)|i|(\%49))((\%6D)|m|(\%4D))((\%67)|g|(\%47))[^\n]+((\%3E)|>)/is

Spanish phone number

/^[9|8|6|7][0-9]{8}$/

IP (IPv4)

^(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)(?:[.](?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)){3}$

IP (IPv6)

/^\s*((([0-9A-Fa-f]{1,4}:){7}(([0-9A-Fa-f]{1,4})|:))|(([0-9A-Fa-f]{1,4}:){6}(:|((25[0-5]|2[0-4]\d|[01]?\d{1,2})(\.(25[0-5]|2[0-4]\d|[01]?\d{1,2})){3})|(:[0-9A-Fa-f]{1,4})))|(([0-9A-Fa-f]{1,4}:){5}((:((25[0-5]|2[0-4]\d|[01]?\d{1,2})(\.(25[0-5]|2[0-4]\d|[01]?\d{1,2})){3})?)|((:[0-9A-Fa-f]{1,4}){1,2})))|(([0-9A-Fa-f]{1,4}:){4}(:[0-9A-Fa-f]{1,4}){0,1}((:((25[0-5]|2[0-4]\d|[01]?\d{1,2})(\.(25[0-5]|2[0-4]\d|[01]?\d{1,2})){3})?)|((:[0-9A-Fa-f]{1,4}){1,2})))|(([0-9A-Fa-f]{1,4}:){3}(:[0-9A-Fa-f]{1,4}){0,2}((:((25[0-5]|2[0-4]\d|[01]?\d{1,2})(\.(25[0-5]|2[0-4]\d|[01]?\d{1,2})){3})?)|((:[0-9A-Fa-f]{1,4}){1,2})))|(([0-9A-Fa-f]{1,4}:){2}(:[0-9A-Fa-f]{1,4}){0,3}((:((25[0-5]|2[0-4]\d|[01]?\d{1,2})(\.(25[0-5]|2[0-4]\d|[01]?\d{1,2})){3})?)|((:[0-9A-Fa-f]{1,4}){1,2})))|(([0-9A-Fa-f]{1,4}:)(:[0-9A-Fa-f]{1,4}){0,4}((:((25[0-5]|2[0-4]\d|[01]?\d{1,2})(\.(25[0-5]|2[0-4]\d|[01]?\d{1,2})){3})?)|((:[0-9A-Fa-f]{1,4}){1,2})))|(:(:[0-9A-Fa-f]{1,4}){0,5}((:((25[0-5]|2[0-4]\d|
[01]?\d{1,2})(\.(25[0-5]|2[0-4]\d|[01]?\d{1,2})){3})?)|((:[0-9A-Fa-f]{1,4}){1,2})))|(((25[0-5]|2[0-4]\d|[01]?\d{1,2})(\.(25[0-5]|2[0-4]\d|[01]?\d{1,2})){3})))(%.+)?\s*$/

URL
I use a PHP code:

    $host = "( [a-z\d]  [-a-z\d] * [a-z\d] \.)+ [a-z]  [-a-z\d] * [a-z] ";
    $port = "(:\d{1,})?";
    $path = "(\/ [^?<>\#\"\s] +)?";
    $query = "(\? [^<>\#\"\s] +)?";
 
    $reg = "#((ht|f)tps?:\/\/{$host}{$port}{$path}{$query})#i";
    return preg_replace($reg, "$1", $url);

Credit Card Number

^((67d{2})|(4d{3})|(5[1-5]d{2})|(6011))(-?s?d{4}){3}|(3[4,7]) d{2}-?s?d{6}-?s?d{5}$

Spanish/European postal code

^([1-9]{2}|[0-9][1-9]|[1-9][0-9])[0-9]{3}$