Regular Expressions 101

Community Patterns

Community Library Entry

0

Regular Expression
PCRE (PHP <7.3)

/
(https?\:\/\/(?:[[:alnum:]]+\.)?[[:alnum:]]+\.(?:com|net|org)\/\S*)|(https?\:\/\/(?:\d{1,3}\.){3}\d{1,3}\/?(?:\:\d{0,6})?\/?)
/
isgm

Description

This pcre style regex tests for valid URLs capturing the whole address. It will capture http and https only. Compatible with multiple sub-domains and raw IPV4 addresses, as well as paths, queries, and parameters. See below for examples:

Will match these valid URLS:

https://www.example.com/foo/?bar=baz&inga=42&quux http://foo.com/blah_(wikipedia)#cite-1 http://foo.com/blah_(wikipedia)_blah#cite-1 http://foo.com/unicode_(✪)_in_parens http://foo.com/(something)?after=parens http://code.google.com/events/#&product=browser http://142.42.1.1/ http://142.42.1.1:8080/

Will NOT match though valid URLS

http://✪df.ws/123 http://userid:password@example.com:8080 http://userid:password@example.com:8080/ http://userid@example.com http://userid@example.com/ http://userid@example.com:8080 http://userid@example.com:8080/ http://userid:password@example.com http://userid:password@example.com/

Does NOT and should not match these bad URLS:

http:// http://. http://.. http://../ http://? http://?? http://??/ http://# http://## http://##/ http://foo.bar?q=Spaces should be encoded // //a ///a /// http:///a foo.com rdar://1234 h://test http:// shouldfail.com :// should fail http://foo.bar/foo(bar)baz quux ftps://foo.bar/ http://-error-.invalid/ http://a.b--c.de/ http://-a.b.co http://a.b-.co http://3628126748 http://.www.foo.bar/ http://www.foo.bar./ http://.www.foo.bar./

Its not perfect, it will match http://999.999.999.999 and similar invalid IPV4 addresses and is not optimized but met my needs.

Submitted by 1nc3p710n - 6 years ago