import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "^(?<Scheme>[file|ftp|https{0,1}|ldap|telnet]+:\\/{2})?(?<Subdomain>[a-zA-Z0-9-]{0,4}\\.)?(?<Domain>\\w+\\.\\w+\\.?\\w+\\/)(?<Subdirectory>@[\\w.]+\\/)?(?<Path>[\\w_~.-]+\\/?[\\w_-]+\\/{0,1}?[\\w_~.-]+)(?<Query>[%?&;].[\\w&_~.=-]+)?(?<Fragment>#?[\\/\\w_-~.,=&-]+)?$";
final String string = "https://medium.com/@s.vive00/7-changes-i-experienced-after-going-1-week-without-added-sugar-9bc48b667f87?source=email-8f87fbd013ca-1581664260785-digest.weekly------0-58------------------2882ae9f_a61e_4d5c_9f87_24f3e0d54cc9-1-----§ionName=top\n\n"
+ "https://medium.com/@DrAyala/exercise-is-the-answer-heres-why-it-s-a-weight-loss-underperformer-8b64103b8c8f?source=email-8f87fbd013ca-1581664260785-digest.weekly------1-59------------------2882ae9f_a61e_4d5c_9f87_24f3e0d54cc9-1-----§ionName=top#Fragment-identifier\n\n"
+ "https://www.medium.com/@DrAyala/exercise-is-the-answer-heres-why-it-s-a-weight-loss-underperformer-8b64103b8c8f?source=email-8f87fbd013ca-1581664260785-digest.weekly------1-59------------------2882ae9f_a61e_4d5c_9f87_24f3e0d54cc9-1-----§ionName=top#Fragment-identifier\n\n\n"
+ "http://www.yoursite.com/path/to/file.mp3\n\n"
+ "http://example.com/data.csv#row=4\n\n"
+ "http://example.com/bar.webm#t=40,80&xywh=160,120,320,240\n\n"
+ "example.com/data.csv#row=4\n\n"
+ "example.co.uk/data.csv#row=4";
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