Created
October 19, 2012 18:44
-
-
Save DHuckaby/3919930 to your computer and use it in GitHub Desktop.
Extract urls from plaintext
This file contains 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
public class Extractor { | |
public static ArrayList<String> extract(String text) { | |
ArrayList<String> links = new ArrayList<String>(); | |
String regex = "\\(?\\b(http://|www[.])[-A-Za-z0-9+&@#/%?=~_()|!:,.;]*[-A-Za-z0-9+&@#/%=~_()|]"; | |
Pattern p = Pattern.compile(regex); | |
Matcher m = p.matcher(text); | |
while (m.find()) { | |
String urlStr = m.group(); | |
if (urlStr.startsWith("(") && urlStr.endsWith(")")) { | |
urlStr = urlStr.substring(1, urlStr.length() - 1); | |
} | |
links.add(urlStr); | |
} | |
return links; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment