问题
How can I write a regular expression to match URLs that contain emojis? The regex should match ordinary alphanumeric URLs along with URLs containing emojis in the domain name, path and/or parameters such as:
http://🏹.to
http://www.🏹.to/🚀🌖
http://🏹.to/I❤️coding?ref=🏹
Most web applications today (including, but not limited to, social networks, online communities, etc.) detect automatically URLs posted by the users as text and convert them into hyperlinks. Nonetheless, almost none of them (StackOverflow included) properly matches URLs that include emojis such as the examples reported above, which are valid, 100% working and which are becoming more and more popular. For all these reasons, the current question is very relevant in terms of code implementation of more modern URL matching regular expressions using any programming language.
回答1:
This regular expression matches ordinary alphanumeric URLs along with URLs containing emojis in the domain name, path and/or parameters:
https?:\/\/(www\.)?[-a-zA-Z0-9\u1F60-\uFFFF@:%._\+~#=]{2,256}\.[a-z]{2,256}\b([-a-zA-Z0-9\u1F60-\uFFFF@:%_\+.~#?&//=]*)
Try it out here: https://regexr.com/3gsl9
Notice that the range of unicode characters that include emojis (i.e. \uXXXX-\uXXXX) might need to be updated in future when new emojis will be added.
回答2:
http://\S+
Where \S+ captures all non whitespace
The trick is keeping the regex from being too greedy, you may need some additional info to help determine the end of the url, is it whitespace or encapsulated in some way?
回答3:
Regex to check URL contains Alphabets
String alphabets= "(.*[a-zA-Z].*)";
Regex to check URL contains Numbers
String numbers= "(.*[0-9].*)";
Regex to check URL contains special characters
String special = "(.*[!,@,$,%,^,&,*,#,~,`,{,},%,|,(,),-,_,=,+,[,],;,:,',\",,,<,.,>,/,?].*$)";
Regex to check URL contains alphanumeric and emojis
String emo="^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%^&()-*/!+=])(?=\\S+$).{size,}$";
Adjust size and change the special characters as per your need.
来源:https://stackoverflow.com/questions/46541831/how-to-match-a-url-containing-emojis-using-a-regex