开发者

how to use Regular Expression match tld in url?

开发者 https://www.devze.com 2023-02-21 16:43 出处:网络
how to use Regular Expressionmatch 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 m

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)

0

精彩评论

暂无评论...
验证码 换一张
取 消