how to use Regular Expression match tld in url?
how to use Regular Expression match tld in url?
Need to match the tld, including almost all countries, organizations. Can do without the regular expression, bu开发者_JAVA百科t requires an efficient match
Do you need to use a regex? Often using a regular expression is overkill. A few lines of code will be faster, and more maintainable than a big regular expression.
If your language has a split method just use that on a "."
and the tld will be the last item in the array. If you're stuck in C++ or something just search backward from the end of the string to the first .
, then the rest of the string from that point is the tld.
arr = url.split(".")
tld = arr[length - 1]
or
int period = url.find_from_last('.');
tld = url.substring(period, npos);
(I forgot the exact syntax for C++ std::string
, but something similar to above)
精彩评论