Udara S.S Liyanage
Udara S.S Liyanage

Reputation: 6453

Regular expression one or more groups - regex to match a url with and without port number

I have a regular expression to match a URL

(^http.?://\b)(.*):(\d*)(.*)

http://udara.com:8907/phpmyadmin/index.php

matches the above expression. However, there may be cases where port is not specified in the URL as below:

http://udara.com/phpmyadmin/index.php?token=48bdb70fd4f1e6abe5ecb84192c1835e

In this case the expression does not match.

How to say zero or more of the 3rd group. Note that there may be IPs instead of the domain udara.com

Upvotes: 0

Views: 75

Answers (2)

AlexR
AlexR

Reputation: 115388

Try the following: (^http.?://([a-zA-Z\-]+)(?::(\d*))?(.*)

EIDT:

^http.?://([a-zA-Z\-\.]+)(?::(\\d*))?(.*) - java

^http.?:\/\/([a-zA-Z\-\.]+)(?::(\d*))?(.*) - perl (and regex101.com)

Upvotes: 1

Savv
Savv

Reputation: 431

This might help:

http://[a-z\.]+(:[0-9]+)*/.+

Upvotes: 0

Related Questions