I need this regular expression:
(https?:\/\/(?:w{1,3}.)?[^\s]*?(?:\.[a-z]+)+)(?![^<]*?(?:<\/\w+>|\/?>))
to match this pattern http://localhost:3000 or any url that has a port number.
Link to rubular https://rubular.com/r/tkCOv181H2KJtU
I need this regular expression:
(https?:\/\/(?:w{1,3}.)?[^\s]*?(?:\.[a-z]+)+)(?![^<]*?(?:<\/\w+>|\/?>))
to match this pattern http://localhost:3000 or any url that has a port number.
Link to rubular https://rubular.com/r/tkCOv181H2KJtU
There are a few things to note in the pattern.
You have to escape the dot to match it literally in this part (?:w{1,3}\.)?
If you add the dot to the character class [^\s.]* you don't have to make it a non greedy quantifier.
You can omit the outer capturing group if you want the match only.
You could make the port part optional (?::\d+)? to match it:
https?:\/\/(?:w{1,3}\.)?[^\s.]+(?:\.[a-z]+)*(?::\d+)?(?![^<]*(?:<\/\w+>|\/?>))
https?:\/\/(?:w{1,3}\.)?[^\s.]*(?:\.[a-z]+)*(?::\d+)?(?![^<]*(?:<\/\w+>|\/?>)) rubular.com/r/aD3ihZWBhm6Mvdhttps://www.google.com/test/ does not match the pattern with this Regexphttps?:\/\/(?:w{1,3}\.)?[^\s.]+(?:\.[a-z]+)*(?::\d+)?(?:\/\w+)*\/?(?![^<]*(?:<\/\w+>|\/?>)) rubular.com/r/RejF1KoE7xsgjs((?:\/\w+)|(?:-\w+))* using an unroll the loop variant (?:\/\w+(?:-\w+)*)* like https?:\/\/(?:w{1,3}\.)?[^\s.]+(?:\.[a-z]+)*(?::\d+)?(?:\/\w+(?:-\w+)*)*(?![^<]*(?:<\/\w+>|\/?>)) See rubular.com/r/Ex7NjbEuhvhrOBTo sum up the Regular expression that matches the requirements is:
https?:\/\/(?:w{1,3}\.)?[^\s.]+(?:\.[a-z]+)*(?::\d+)?((?:\/\w+)|(?:-\w+))*\/?(?![^<]*(?:<\/\w+>|\/?>))
This expression includes the following characters in the url:
https?:\/\/[a-z]+(?:\.[a-z]+)*(:\d+)?(?:\/[a-z\d]+)*https?:\/\/(?:w{1,3}\.)?[^\s.]*(?:\.[a-z]+)+(?::\d+)?(?![^<]*(?:<\/\w+>|\/?>))regex101.com/r/Yx6qJZ/1