$re = '`(?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>#.*)?`m';
$str = 'https://www.geeksforgeeks.org/lru-cache-implementation/#hastest?name=234ksldjfkl&sjkldf=
https://developer.ibm.com/series/kubernetes-learning-path/
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
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';
preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);
// Print the entire match result
var_dump($matches);
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 PHP, please visit: http://php.net/manual/en/ref.pcre.php