Last active
December 16, 2015 12:38
-
-
Save moskinson/5435404 to your computer and use it in GitHub Desktop.
Url regex in Groovy with metaclass for String clazz return a list of all urls that string contains
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| private addExtractUrls(){ | |
| String.metaClass.extractUrls = { -> | |
| def urlsList = [] | |
| def matchesUrl = delegate =~ regExForUrls() | |
| matchesUrl.each{ matches -> | |
| urlsList << matches[0] | |
| } | |
| return urlsList | |
| } | |
| } | |
| private regExForUrls(){ | |
| /([a-zA-Z0-9]+[.](aero|asia|biz|cat|com|coop|edu|es|gov|info|int|jobs|mil|mobi|museum|name|net|org|pro|tel|travel|xxx)[-a-zA-Z0-9+&@#\/%?=~_|:.,;]*)|((https?:\/\/)?([-a-zA-Z0-9_]+[.])*[-a-zA-Z0-9_]+[.][-a-zA-Z0-9_]+[.][a-zA-Z]+[a-zA-Z]+[\-a-zA-Z0-9+&@#\/%?=~_|:\.\,;]*)|(\b(https?:\/\/)([-a-zA-Z0-9_]+[.])*[-a-zA-Z0-9_]+[.][a-zA-Z]+[a-zA-Z]+[\-a-zA-Z0-9+&@#\/%?=~_|:\.\,;]*)/ | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment