PCRE for URL / Hostname match

aish13
aish13 used Ask the Experts™
on
Hi -

I am looking for a PCRE expression which can match something like this -

http://[InputExpression] OR https://[InputExpression]
http://[InputExpression]:80 OR https://[InputExpression]:443
http://[InputExpression]/ OR https://[InputExpression]/
http://[InputExpression]:80/ OR https://[InputExpression]:443/

[InputExpression] value could be either "myashish" or an IPAddress.

Basically PCRE should only match the above. If there is anything else in the URL e.g. http://mypgetrain/helloworld, http://mypgetrain/helloworld/cust etc (like a URI at the end) then it should not match. Only if the input value is one of the above URLs then it should create a match.

Any help in this regards will be really appreciated.

Regards
Ashish
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2009

Commented:
If you are using perl, you can use something other than / as the delimiter, making it easier to read.  This will work:
    m#^https?://[\w\.]+(:80|:443)?/?$#

If not, and you have to use / as the delimiter, then the / needs to be escaped in the RE:
    /^https?:\/\/[\w\.]+(:80|:443)?\/?$/
Terry WoodsIT Guru
Most Valuable Expert 2011

Commented:
$pattern = "@https?://".preg_quote($inputExpression,"@").":(?:80|443)?/?@";

if (preg_match($pattern, $url)) {
  print "Great";
} else {
  print "Blurgh";
}
Terry WoodsIT Guru
Most Valuable Expert 2011

Commented:
Seeing Adam314's suggestion, you'll want the start and end-of-line placemarkers too:

$pattern = "@^https?://".preg_quote($inputExpression,"@").":(?:80|443)?/?$@";
CompTIA Network+

Prepare for the CompTIA Network+ exam by learning how to troubleshoot, configure, and manage both wired and wireless networks.

Terry WoodsIT Guru
Most Valuable Expert 2011

Commented:
Apologies - there was a mistake in my suggestions. This should do it:

$pattern = "@^https?://".preg_quote($inputExpression,"@")."(?:\:(?:80|443))?/?$@";

Author

Commented:
Hi - Will it be possible for you create a PCRE that would have basically have input expression hardcoded as "mycompanytrain"...I am very new to PCRE and couldn't figure out how to do it.

Regards
Ashish

IT Guru
Most Valuable Expert 2011
Commented:
The best way to code it is like this:

$inputExpression = "mycompanytrain";
$pattern = "@^https?://".preg_quote($inputExpression,"@")."(?:\:(?:80|443))?/?$@";

But you could hardcode it to your pattern if you really wanted:

$pattern = "@^https?://mycompanytrain(?:\:(?:80|443))?/?$@";
Terry WoodsIT Guru
Most Valuable Expert 2011

Commented:
preg_quote escapes any regular special characters that are valid characters in the URL, such as full stops. You'll need to use the preg_quote version if you want full stops in your input expression, unless you manually escape them.

eg if your input expression was google.co.uk, you could use either:

$inputExpression = "google.co.uk";  #just this changed... very easy!
$pattern = "@^https?://".preg_quote($inputExpression,"@")."(?:\:(?:80|443))?/?$@";

or the following, manually escaping the full stops (not the best way):
$pattern = "@^https?://google\.co\.uk(?:\:(?:80|443))?/?$@";
Terry WoodsIT Guru
Most Valuable Expert 2011

Commented:
Typo: "regular special characters" was supposed to say "regular expression special characters"

Author

Commented:
Hi - Thanks a lot for the solution. The below URL pattern which you gave brought the following results -

"@^https?://mypgetrain(?:\:(?:80|443))?/?$@"

MATCHES
http://mycompanytrain
http://mycompanytrain/
http://mycompanytrain:80
http://mycompanytrain:80/
https://mycompanytrain
https://mycompanytrain/
https://mycompanytrain:443
https://mycompanytrain:443/

DID NOT MATCH
http://172.21.141.208
http://172.21.141.208/
http://172.21.141.208:80
http://172.21.141.208:80/
https://172.21.141.208
https://172.21.141.208/
https://172.21.141.208:443
https://172.21.141.208:443/

Can you please change the pattern so that it matches the IP address as well?

Regards
Ashish
Terry WoodsIT Guru
Most Valuable Expert 2011
Commented:
So in that case is your input expression going to be 172.21.141.208 or do you need PHP to look up the IP address itself?

If your input expression is 172.21.141.208 then:
$inputExpression = "172.21.141.208";  #just this changed... very easy!
$pattern = "@^https?://".preg_quote($inputExpression,"@")."(?:\:(?:80|443))?/?$@";

or $pattern = "@^https?://172\.21\.141\.208(?:\:(?:80|443))?/?$@"; #full stops in the IP address are manually escaped

Author

Commented:
Thanks a lot for help...

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial