import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "(?<scheme>https?|s?ftp|wss?|git):\\/\\/(?<subDomain>[a-z0-9-]{1,63}\\.(?:[a-z0-9-]{1,63}\\.)*)?(?<domain>[a-z0-9-]{1,256})[.](?<tld>[a-z0-9]+)(?::(?<port>[0-9]{1,5}))?(?<path>\\/.*/?)?";
final String string = "https://www.google.com\n"
+ "https://google.com:65535\n"
+ "https://www.google.com/search\n"
+ "https://google.com/search\n"
+ "https://www.google.com/\n"
+ "https://google.com/\n\n"
+ "http://www.google.com\n"
+ "http://google.com\n"
+ "http://www.google.com/search\n"
+ "http://google.com/search\n"
+ "http://www.google.com/\n\n"
+ "http://google.com/\n\n\n"
+ "sftp://\n"
+ "ftp://\n"
+ "ws://\n"
+ "wss://\n"
+ "git://\n"
+ "git://\n\n"
+ "https://my.website-is.cool.name:5673/thing?456%20asd#foo";
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