Regular Expressions 101

Community Patterns

Parse URL then Breakdown into parts

0

Regular Expression
ECMAScript (JavaScript)

/
(?<protocol>(?:https?:\/\/?)+(?:www(?!.))?)?(?<tdl>[a-zA-Z0-9@:._-]{2,256}\.[a-z0-9]{2,6}(?:[:0-9]{1,5})?)(?<path>(?:\/)[-a-zA-Z0-9\/_]+)?(?<params>[-a-zA-Z0-9?%#+()=&]+)?
/
gm

Description

Will parse a valid URL and break it down into its parts. Making it easier to handle for checking and anything else really in any given langage

The following link is a hastebin example of the parsing in Java

It will output the following https://haste.readonly.network/viheradehe.java

Match 1:

tdl: google.com


Match 2:

protocol: http:// tdl: foo.com path: /blah_blah


Match 3:

protocol: http:// tdl: foo.com path: /blah_blah/


Match 4:

protocol: http:// tdl: foo.com:


Match 5:

protocol: http:// tdl: foo.com:8080 path: /blah_blah


Match 6:

protocol: https:// tdl: www.example.com path: /foo/ params: ?bar=baz&inga=42&quux


Match 7:

protocol: https:// tdl: www.example.com path: /wpstyle/ params: ?p=364


Match 8:

protocol: https:// tdl: www.example.com path: /wpstyle/wpath/ params: ?p=364&?param=this


Match 9:

tdl: www.google.com


Submitted by ROMVoid95 - 3 years ago