12

I need to check the web address, using regular expression.

If user type URL as

  1. www.test.com
  2. http://www.test.com
  3. https://www.test.com

I have a regular expression like

/^(http\:\/\/[a-zA-Z0-9_\-]+(?:\.[a-zA-Z0-9_\-]+)*\.[a-zA-Z]{2,4}(?:\/[a-zA-Z0-9_]+)*(?:\/[a-zA-Z0-9_]+\.[a-zA-Z]{2,4}(?:\?[a-zA-Z0-9_]+\=[a-zA-Z0-9_]+)?)?(?:\&[a-zA-Z0-9_]+\=[a-zA-Z0-9_]+)*)$/

but it will only allow the second option only. How can I modify the regular expression so, that it accepts 1st and 3rd option too?

Andy A.
  • 1,392
  • 5
  • 15
  • 28
Linto davis
  • 702
  • 2
  • 9
  • 16

7 Answers7

24

This is a pretty basic expression for testing domain names:

@^(http\:\/\/|https\:\/\/)?([a-z0-9][a-z0-9\-]*\.)+[a-z0-9][a-z0-9\-]*$@i

Should match:

domain.com
www.domain.com
http://domain.com
https://domain.com
Salman A
  • 262,204
  • 82
  • 430
  • 521
3

Use the following program for validating the website URL.

It will be validating the following and any valid website URL.

  1. www.test.com
  2. http://www.test.com
  3. https://www.test.com

    <?php
            $string_url="https://www.test.com";
            $reg_exp = "/^(http(s?):\/\/)?(www\.)+[a-zA-Z0-9\.\-\_]+(\.[a-zA-Z]{2,3})+(\/[a-zA-Z0-9\_\-\s\.\/\?\%\#\&\=]*)?$/";
            if(preg_match($reg_exp, $string_url) == TRUE){
            echo "URL is valid format";
            }
            else{
            echo "URL is invalid format";
            }
    ?>


Josh Lee
  • 171,072
  • 38
  • 269
  • 275
rekha_sri
  • 2,677
  • 1
  • 24
  • 27
2

You can use this pattern also:

(http(s)?://)?([\w-]+\.)+[\w-]+(/[\w- ;,./?%&=]*)?

This will work for matching:

http://www.google.com
https://www.google.com
www.google.com
google.com
google.in

Simple pattern for matching.

Hope this will help someone!!!

Mohd Haider
  • 673
  • 1
  • 5
  • 25
  • Close, but fails with this test data "2020-10-27 18:00:10.128" – Brendan Kim Mar 18 '22 at 22:33
  • @BrendanKim, I am not sure about this test data. This simple regex will work for website url checking and looks like you are checking date string. So it will not work for this. Let me know if I am missing something here. – Mohd Haider Mar 24 '22 at 10:39
1

You can make the http:// optional and also the s in https optional as:

/^((?:http(?:s)?\:\/\/)?[a-zA-Z0-9_-]+(?:.[a-zA-Z0-9_-]+)*.[a-zA-Z]{2,4}(?:\/[a-zA-Z0-9_]+)*(?:\/[a-zA-Z0-9_]+.[a-zA-Z]{2,4}(?:\?[a-zA-Z0-9_]+\=[a-zA-Z0-9_]+)?)?(?:\&[a-zA-Z0-9_]+\=[a-zA-Z0-9_]+)*)$/
codaddict
  • 445,704
  • 82
  • 492
  • 529
1

Pattern

(https?:\/\/)?([\w\d]+\.)?[\w\d]+\.\w+\/?.+

This will work for matching:

www.demo.com

http://foo.co.uk/

Swadesh Ranjan Dash
  • 544
  • 1
  • 4
  • 17
0

Try the following instead :

filter_var($your_variable, FILTER_VALIDATE_URL);
Stephan
  • 41,764
  • 65
  • 238
  • 329
1ce
  • 1
0

Well my Regular Expression will with all type of URL.

^(http(s?):\/\/)?(www\.)?+([a-zA-Z0-9\.\-\_]+)(\.[a-zA-Z]{2,3})+(\/[a-zA-Z0-9\_\-\s\.\/\?\%\#\&\=]*)?

Tap to see the Proof with test cases passed

Syakur Rahman
  • 2,056
  • 32
  • 40