Start Regular expression for validating hostname

Regular expression for validating hostname

Hope this can help you when you want to know which host of a URL belongs to.

If you want to check whether a string is a URL or not. In Java Script, we can have a regular expression like var pattern=/(. ([^\/] )(\/.*)*/i;the regular expression pattern can be used to get the hostname.

The second parenthesis is to match the hostname, anything before the first occurrence of '/' after the protocol string will belong to the hostname.

If no '/' present, then the whole string after the protocol string is the hostname.

The third parenthesis is to match all the rest after the hostname.

* * Author: Alban Diquet * License: See LICENSE * */ // Get rid of OSX 10.7 and greater deprecation warnings.

#if defined(__APPLE__) && defined(__clang__) #pragma clang diagnostic ignored "-Wdeprecated-declarations" #endif #include #include "openssl_hostname_validation.h" #include "hostcheck.h" #define HOSTNAME_MAX_SIZE 255 /** * Tries to find a match for hostname in the certificate's Common Name field. * Returns Match Not Found if no matches were found.

Then we can retrieve the hostname from the returned array.

There are three parenthesis in the pattern, they are used to group the strings together and when testing on the target string, the matched string blocks can be remembered and returned as an array.

Sometimes we may have strings which contain some UR;s and we may want to retrieve the hostname from the URLs for some statistic use. It can have zero or one occurrence of the protocol in one valid URL.

The first parenthesis is to check the protocol of the URL, protocol can be https://,ftp:// or file://.

It is the same if the URL doesn't have /or any other similar blocks appended, in this case arr[3] will be empty.