0

i want to match the url with contatin http/https and (www) or without (www)with the help of regular expression. following is my regualr expression

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

but in this, the empty string is not work for example : when i enter http://www.google.com is valid url but http://google.com is not valid. thanks for the answer in advance.

1 Answer 1

1

You can't search for an not existing part using ^$. that is expecting a start of a string(row), end of the string (row).

Just make the "www" part optional:

^((https?)://)(www\\.)?(\.[a-z0-9-]+)+([/?].*)?$

another problem is, you are searching for two dots in a row:

^((https?)://)(www\\.)?(\.[a-z0-9-]+)+([/?].*)?$
                    ^    ^

I think a better pattern would be

^((https?)://)(www\\.)?([a-z0-9-]+)(\.[a-z0-9-]+)*([/?].*)?$

Are you aware, that you are excluding a lot of valid URLS, by only allowing the characters [a-z0-9-]?

Sign up to request clarification or add additional context in comments.

4 Comments

thanks for this , but i want to restricted (www) only ,in this the other character also accept like 'sss'
How to you want to know to what part of the domain the character sequence belongs? e.g. http://www.sss.com versus http://sss.com?
so @stema what i do, or please give me another regular expression , which validate all valid url.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.