import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "href=[\"'](https?:\\/\\/(?:www\\.|(?!www))[^\\s\\.]+\\.[^\\s\"']{2,}|(?!ftp:\\/\\/)[^\\s]+\\.[^\\s'\"]{2,})";
final String string = "Yes:\n"
+ "http://google.it\n"
+ "<a href=\"http://www.it.com\">algo</a>\n"
+ "https://something.com\n"
+ "https://www.something.com\n"
+ "<a href=\"iaaaaaaaat.com\">hola</a>\n"
+ "http://something.com\n"
+ "http://www.something.com\n"
+ "<a href='www.it.com'>hola</a>\n"
+ "https://something.gov\n"
+ "<a href='https://www.something.gov'>Hola</a>\n"
+ "http://something.gov\n"
+ "<a href=\"ftp://longurljapanese.jp\">hollaaaa</a>\n\n"
+ "<a href=\"it.it\">hollaaaa</a>\n"
+ "http://www.something.gov\n\n"
+ "https://something.org\n"
+ "https://www.something.org\n"
+ "http://something.org\n"
+ "http://www.something.org\n\n"
+ "https://something.net\n"
+ "https://www.something.net\n"
+ "http://something.net\n"
+ "http://www.something.net\n\n"
+ "https://something.edu\n"
+ "https://www.something.edu\n"
+ "http://something.edu\n"
+ "http://www.something.edu\n\n"
+ "https://something.biz\n"
+ "https://www.something.biz\n"
+ "http://something.biz\n"
+ "http://www.something.biz\n\n"
+ "something.biz\n"
+ "www.something.biz\n\n"
+ "something.edu\n"
+ "www.something.edu\n\n"
+ "something.net\n"
+ "www.something.net\n\n"
+ "something.org\n"
+ "www.something.org\n\n"
+ "something.gov\n"
+ "www.something.gov\n\n"
+ "something.com\n"
+ "www.something.com\n\n\n"
+ "No:\n\n"
+ "https://www.something\n"
+ "http://www.something";
final Pattern pattern = Pattern.compile(regex);
final Matcher matcher = pattern.matcher(string);
while (matcher.find()) {
System.out.println("Full match: " + matcher.group(0));
for (int i = 1; i <= matcher.groupCount(); i++) {
System.out.println("Group " + i + ": " + matcher.group(i));
}
}
}
}
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Java, please visit: https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html