I am trying to get this regex dialed-in to validate whether a URL begins with https and if a port is supplied the only valid values are 443 or 5443. This regex is pretty close but not quite there.

^(https:\/\/)([a-zA-Z\d\.]{2,})\.([a-zA-Z]{2,})(:5{0,1}443)?(.)*

How do I solve this problem?

3 Answers

1
Pushpesh Kumar Rajwanshi On Best Solutions

You should append a / after this optional port group so it doesn't allow any digits before a /. Try using this regex,

^(https:\/\/)([a-zA-Z\d\.]{2,})\.([a-zA-Z]{2,})(:5?443)?\/\S*

Notice, I've also changed (:5{0,1}443)? to (:5?443)? and changed last .* to \S* so the url doesn't capture spaces as spaces in URL is not a valid thing. Besides that, you can also get rid of so many groups in your regex, unless you need them.

Regex Demo

Edit:

As you said in comments, that you want to match following URLs too,

https://example.com
https:example.com
https:example.com:443

you need to make \/\S* part optional by placing a ? after them. The modified regex becomes this, which will match above URLs.

^https:\/\/([a-zA-Z\d\.]{2,})\.([a-zA-Z]{2,})(:5?443)?(\/\S*)?

Demo with filepath part being optional

-1
Emma On

Your RegEx seems to work okay. You may try using this RegEx and add additional boundaries, just for safety, if you wish so:

^(https:\/\/)([a-zA-Z\d\.]{2,})\.([a-zA-Z]{2,}):(5443|443)?$

enter image description here

I only added a $ end char so that to bound your original expression from the right. You may just simply add a few port numbers, if you may have, in this capturing group:

(5443|443) 

You can also remove unnecessary boundaries, if you wish.

0
sln On

This is a mainstream URL validator that tests if it's between whitespace boundary's.
It only allows https device and the port numbers 5443 or 443.

(?<!\S)https://(?:\S+(?::\S*)[email protected])?(?:(?:(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\u00a1-\uffff0-9]+-?)*[a-z\u00a1-\uffff0-9]+)(?:\.(?:[a-z\u00a1-\uffff0-9]+-?)*[a-z\u00a1-\uffff0-9]+)*(?:\.(?:[a-z\u00a1-\uffff]{2,})))|localhost)(?::5?443)?(?:/[^\s]*)?(?!\S)

Readable version

 (?<! \S )

 https ://
 (?:
      \S+ 
      (?: : \S* )?
      @
 )?
 (?:
      (?:
           (?:
                [1-9] \d? 
             |  1 \d\d 
             |  2 [01] \d 
             |  22 [0-3] 
           )
           (?:
                \.
                (?: 1? \d{1,2} | 2 [0-4] \d | 25 [0-5] )
           ){2}
           (?:
                \.
                (?:
                     [1-9] \d? 
                  |  1 \d\d 
                  |  2 [0-4] \d 
                  |  25 [0-4] 
                )
           )
        |  (?:
                (?: [a-z\u00a1-\uffff0-9]+ -? )*
                [a-z\u00a1-\uffff0-9]+ 
           )
           (?:
                \.
                (?: [a-z\u00a1-\uffff0-9]+ -? )*
                [a-z\u00a1-\uffff0-9]+ 
           )*
           (?:
                \.
                (?: [a-z\u00a1-\uffff]{2,} )
           )
      )
   |  localhost
 )
 (?: : 5? 443 )?
 (?: / [^\s]* )?

 (?! \S )