Regular expression pattern to match url with or without http://www

Regular expression pattern to match url with or without http://www

preg_match url pattern
regex match url path
url regex
simple url regex
url validation regex python
url regex pattern java
java regex match url path
regex url validation

I'm not very good at regular expressions at all.

I've been using a lot of framework code to date, but I'm unable to find one that is able to match a URL like http://www.example.com/etcetc but also is able to catch something like www.example.com/etcetc and example.com/etcetc.

Any help would be great. Thanks guys!


For matching all kind of URLs following code should work:

<?php
    $regex = "((https?|ftp)://)?"; // SCHEME
    $regex .= "([a-z0-9+!*(),;?&=$_.-]+(:[a-z0-9+!*(),;?&=$_.-]+)?@)?"; // User and Pass
    $regex .= "([a-z0-9\-\.]*)\.(([a-z]{2,4})|([0-9]{1,3}\.([0-9]{1,3})\.([0-9]{1,3})))"; // Host or IP
    $regex .= "(:[0-9]{2,5})?"; // Port
    $regex .= "(/([a-z0-9+$_%-]\.?)+)*/?"; // Path
    $regex .= "(\?[a-z+&\$_.-][a-z0-9;:@&%=+/$_.-]*)?"; // GET Query
    $regex .= "(#[a-z_.-][a-z0-9+$%_.-]*)?"; // Anchor
?>

Then, the correct way to check against the regex is as follows:

<?php
   if(preg_match("~^$regex$~i", 'www.example.com/etcetc', $m))
      var_dump($m);

   if(preg_match("~^$regex$~i", 'http://www.example.com/etcetc', $m))
      var_dump($m);
?>

Courtesy: Comments made by splattermania on PHP manual: http://php.net/manual/en/function.preg-match.php

RegEx Demo in regex101

Url checker with or without http:// or https://, Top Regular Expressions. Match string not containing string � Check if a string only contains numbers � Match elements of a url � Validate an ip address Match any character \w: Match a word chracter \W: Match a non-word character \d: Match a digit \D: Match any non-digit character \s: Match a whitespace character \S: Match a non-whitespace character \b: Match character at the beginning or end of a word \B: Match a character not at beginning or end of a word \0: Match a NUL character \t: Match a


This works for me in all cases I had tested:

$url_pattern = '/((http|https)\:\/\/)?[a-zA-Z0-9\.\/\?\:@\-_=#]+\.([a-zA-Z0-9\&\.\/\?\:@\-_=#])*/';

Tests:

http://test.test-75.1474.stackoverflow.com/
https://www.stackoverflow.com
https://www.stackoverflow.com/
http://wwww.stackoverflow.com/
http://wwww.stackoverflow.com


http://test.test-75.1474.stackoverflow.com/
http://www.stackoverflow.com
http://www.stackoverflow.com/
stackoverflow.com/
stackoverflow.com

http://www.example.com/etcetc
www.example.com/etcetc
example.com/etcetc
user:pass@example.com/etcetc

example.com/etcetc?query=aasd
example.com/etcetc?query=aasd&dest=asds

http://stackoverflow.com/questions/6427530/regular-expression-pattern-to-match-url-with-or-without-http-www
http://stackoverflow.com/questions/6427530/regular-expression-pattern-to-match-url-with-or-without-http-www/

Every valid internet URL has at least one dot, so the above pattern will simply try to find any at least two string chained by a dot, and has valid characters that URL may have.

url without protocol, Top Regular Expressions. Match string not containing string � Check if a string only contains numbers � Url checker with or without http:// or https:// � Only letters� Every valid internet URL has at least one dot, so the above pattern will simply try to find any at least two string chained by a dot, and has valid characters that URL may have. share | improve this answer | follow |


Try this:

/^http:\/\/|(www\.)?[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$/

It works exactly like the people want.

It takes with or with out http://, https://, and www.

Regular expression for Validating URL with or without http, I am using Regular expression validator for validating URL (of asp.net validators) Here is the expression: http(s)?://([\w-]+\.) http://blog.spicelogic.com/post/ Validate-URL-without-Regex.aspx A regex pattern like this:. Pattern – The rule pattern is used to specify either the regular expression or a wildcard pattern that is used to match URL strings. Conditions – The optional conditions collection is used to specify additional logical operations to perform if a URL string matches the rule pattern. Within the conditions, you can check for certain values of


You can use a question mark after a regular expression to make it conditional so you would want to use:

http:\/\/(www\.)?

That will match anything that has either http://www. or http:// (with no www.)

What you could do is just use a replace method to remove the above, thus getting you the domain. Depends on what you need the domain for.

In search of the perfect URL validation regex, Just imagine you're doing this before testing $url with the regex: These URLs should match ( 1 → correct) http://foo.com/blah_blah_(wikipedia)_(again), 1, 1 � When attempting to build a logical “or” operation using regular expressions, we have a few approaches to follow. Fortunately the grouping and alternation facilities provided by the regex engine are very capable, but when all else fails we can just perform a second match using a separate regular expression – supported by the tool or native language of your choice.


Try something like this:

.*([\w-]+\.)+[a-z]{2,5}(/[\w-]+)*

Perfect URL Regex: The Perfect URL Regular Expression, The Perfect URL Regular Expression. Simply copy and paste the URL regex below for the language of your choice. PHP (with validate filter). if (filter_var($ url SELECT field FROM table WHERE field REGEXP "^(https?://|www\\.)[\. Looks like not so 100% pattern becuase "https://.com" and "http://.com" are still valid. This regular expression pattern can be used to check the validity of paths for file upload controls. The uploaded file can be either stored locally or accessible through UNC. It cannot contain illegal characters for the windows OS - that may be supported e.g. on Mac OS – and cannot be a URL (Yes, as weird as it may seem, some users enter URLs


Regex to match urls (but not without a tld) [Help Request] : regex, Regex to match urls (but not without a tld) [Help Request]. Hi all,. I know there are Start off by matching the http part of the URL: ^https?://. Next, since you are Learn to use JavaScript RegExp with hundreds of examples and exercises. A regular expression (shortened as regex or regexp; also referred to as rational expression) is a sequence of characters that define a search pattern.Usually such patterns are used by string-searching algorithms for "find" or "find and replace" operations on strings, or for input validation.


RegExLib.com, Modified URL RegExp that requires (http, https, ftp,gopher,telnet,file|notes,ms- help)://, A nice domain, and Matches. http://regexlib.com/Add.aspx, http://www. regexlib.com/Add.aspx Without (http\://) it only does the latter check. Browse & Discover Thousands of Computers & Internet Book Titles, for Less.


URL Regex (JS) testing, http://foo.com/blah_blah_(wikipedia)_(again) http://✪df.ws/123 http://userid: password@example.com:8080 Matches the expression before or after the | .