import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "(?:(?:https?|ftp):\\/\\/)?(?:\\S+(?::\\S*)?@)?(?:((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]|[0-9])\\.){3}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]|[0-9])|(?:(?:[a-z\\u00a1-\\uffff0-9]+-)*[a-z\\u00a1-\\uffff0-9]+)(?:\\.(?:[a-z\\u00a1-\\uffff0-9]+-)*[a-z\\u00a1-\\uffff0-9]+)*(?:\\.(?:[a-z\\u00a1-\\uffff]{2,})))(?::\\d{2,5})?(?:\\/[^\\s]*)?";
final String string = "https://face.com\n"
+ "http://face.com\n"
+ "https://www.face.com/\n"
+ "face.com\n"
+ "www.face.com\n"
+ "w3.face.com\n"
+ "0.0.0.0\n"
+ "ac31.23.53.122asd\n"
+ "92.168.202.9712121212121\n\n"
+ "1292.168.202.97\n"
+ "192.168.202.97\n"
+ "224.168.202.97\n"
+ "92.18.2.97\n"
+ "http://74.251.154.247/find-ip-address-of-web-site.html\n\n"
+ "74.201.154.247/find-ip-address-of-web-site.html\n\n"
+ "74.201.154.247\n\n"
+ "foo.com\n\n"
+ "http://92.18.2.97\n\n\n\n"
+ "asasasasasac31.23.53.122asasassasd\n\n"
+ "12312312312321.32.34.2312312312321\n\n"
+ "12.3423423432.234123123.123\n\n"
+ "31.134232131.231.34\n\n\n"
+ "SHOULD MATCH:\n\n"
+ "http://foo.com/blah_blah\n"
+ "http://foo.com/blah_blah/\n"
+ "http://foo.com/blah_blah_(wikipedia)\n"
+ "http://foo.com/blah_blah_(wikipedia)_(again)\n"
+ "http://www.example.com/wpstyle/?p=364\n"
+ "https://www.example.com/foo/?bar=baz&inga=42&quux\n"
+ "http://✪df.ws/123\n"
+ "http://userid:password@example.com:8080\n"
+ "http://userid:password@example.com:8080/\n"
+ "http://userid@example.com\n"
+ "http://userid@example.com/\n"
+ "http://userid@example.com:8080\n"
+ "http://userid@example.com:8080/\n"
+ "http://userid:password@example.com\n"
+ "http://userid:password@example.com/\n"
+ "http://142.42.1.1/\n"
+ "http://142.42.1.1:8080/\n"
+ "http://➡.ws/䨹\n"
+ "http://⌘.ws\n"
+ "http://⌘.ws/\n"
+ "http://foo.com/blah_(wikipedia)#cite-1\n"
+ "http://foo.com/blah_(wikipedia)_blah#cite-1\n"
+ "http://foo.com/unicode_(✪)_in_parens\n"
+ "http://foo.com/(something)?after=parens\n"
+ "http://☺.damowmow.com/\n"
+ "http://code.google.com/events/#&product=browser\n"
+ "http://j.mp\n"
+ "ftp://foo.bar/baz\n"
+ "http://foo.bar/?q=Test%20URL-encoded%20stuff\n"
+ "http://مثال.إختبار\n"
+ "http://例子.测试\n"
+ "http://उदाहरण.परीक्षा\n"
+ "http://-.~_!$&'()*+,;=:%40:80%2f::::::@example.com\n"
+ "http://1337.net\n"
+ "http://a.b-c.de\n"
+ "http://223.255.255.254\n\n";
final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
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