Aust1n46 / VentureChat

GNU General Public License v3.0
37 stars 46 forks source link

Add improved URL regex #123

Open Meeples10 opened 3 weeks ago

Meeples10 commented 3 weeks ago

The current URL regex matches some strings that are not valid URLs, such as 1.0 and ///.///, and fails to match some valid URLs. The new regular expression, while not exactly aligned with the URI format defined in RFC 3986, is much more robust. It also supports Bukkit color codes, unlike the library proposed in #93.

((?:[a-zA-Z&][a-zA-Z+\\-.&]*)://)?(?:([^\\s:@]*)(?::([^\\s@]*))?@)?((?:[a-zA-Z0-9\\-.&]*[a-zA-Z0-9])+\\.[a-zA-Z\\-&]{2,}|(?:[&0-9]{1,3}\\.[&0-9]{1,3}\\.[&0-9]{1,3}\\.[&0-9]{1,3}))(?::([&0-9]{1,5}))?(/[^\\s?]*)*(?:\\?([^\\s]*))?

Sample URL: https://user:pass@github.com:433/Aust1n46/VentureChat?tab=readme-ov-file

Match Group URI Component Sample Match
((?:[a-zA-Z&][a-zA-Z+\-.&]*)://)? Scheme https
(?:([^\s:@]*)(?::([^\s@]*))?@)? Credentials user:pass
((?:[a-zA-Z0-9\-.&]*[a-zA-Z0-9])+\.[a-zA-Z\-&]{2,}\|(?:[&0-9]{1,3}\.[&0-9]{1,3}\.[&0-9]{1,3}\.[&0-9]{1,3})) Host github.com
(?::([&0-9]{1,5}))? Port 443
(/[^\s?]*)* Path /Aust1n46/VentureChat
(?:\?([^\s]*))? Query tab=readme-ov-file