import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "(?P<origin>(?P<protocol>http[s]?:)?\\/\\/(?P<host>[a-z0-9A-Z-_.]+))(?P<port>:\\d+)?(?P<path>[\\/a-zA-Z0-9-\\.]+)?(?P<search>\\?[^#\\n]+)?(?P<hash>#.*)?";
final String string = "https://www.geeksforgeeks.org/lru-cache-implementation/#hastest?name=234ksldjfkl&sjkldf=\n"
+ "https://developer.ibm.com/series/kubernetes-learning-path/\n"
+ "https://www.google.com/search?q=cannot+find+package+%22context%22+in+any+of%3A&oq=cannot+find+package+%22context%22+in+any+of%3A&aqs=chrome..69i57j0l5.205752j0j4&sourceid=chrome&ie=UTF-8#hello world\n\n"
+ "https://myaccount.google.com/name?utm_source=google-account&utm_medium=web&pli=1&rapt=AEjHL4PXc8zLqgFQOXxYQRbdKJI8\\nyNq3u&name=rjl@1239015423#Kkn1HwudmOHSj7MpYAguZoOmrznrl0PF7hyLkZ17xFdrXToG5UDkMhO2bM1a6i5xw#path=234&sp=\\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