1

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

6
  • Are you looking to validate URLs or just search for URL-like strings? Commented Mar 13, 2020 at 17:02
  • https?:\/\/[a-z]+(?:\.[a-z]+)*(:\d+)?(?:\/[a-z\d]+)* Commented Mar 13, 2020 at 17:05
  • 1
    Make the port part optional https?:\/\/(?:w{1,3}\.)?[^\s.]*(?:\.[a-z]+)+(?::\d+)?(?![^<]*(?:<\/\w+>|\/?>)) regex101.com/r/Yx6qJZ/1 Commented Mar 13, 2020 at 17:06
  • That works. Make it an answer and I will validate it. Commented Mar 13, 2020 at 17:08
  • 1
    @LoudghiriAhmed I have updated it. Commented Mar 13, 2020 at 17:23

2 Answers 2

3

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+>|\/?>))

Regex demo

Sign up to request clarification or add additional context in comments.

9 Comments

The patter localhost:3000 is not a match with this Regexp rubular.com/r/ArkswxZDanykvK
@LoudghiriAhmed I forgot a quantifier. Like this https?:\/\/(?:w{1,3}\.)?[^\s.]*(?:\.[a-z]+)*(?::\d+)?(?![^<]*(?:<\/\w+>|\/?>)) rubular.com/r/aD3ihZWBhm6Mvd
https://www.google.com/test/ does not match the pattern with this Regexp
But you could for example extend it to repeat matching word chars and forward slashes https?:\/\/(?:w{1,3}\.)?[^\s.]+(?:\.[a-z]+)*(?::\d+)?(?:\/\w+)*\/?(?![^<]*(?:<\/\w+>|\/?>)) rubular.com/r/RejF1KoE7xsgjs
@LoudghiriAhmed The pattern works indeed. If I could make a suggestion, it would be writing this part with the alternation ((?:\/\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/Ex7NjbEuhvhrOB
|
0

To 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://rubular.com/r/7BjXQP6vaA7hvM

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.