0

I require to validate urls which should be like

google.com  or
yahoo.co.uk etc

I mean I dont require http or www. My regex code is like this.But it doesnt work for me.

/^[a-zA-Z0-9][a-zA-Z0-9-][a-zA-Z0-9]\.[a-zA-Z]{2,}$/
Techy
  • 2,626
  • 7
  • 41
  • 88
  • i normaly use a reg-ex that needs 3 characters, a dot, and then atleast 3 new characters. Might be useful for you – DiederikEEn May 02 '13 at 11:57
  • 1
    Please check this question, it has different URL validators depending on your need: http://stackoverflow.com/questions/161738/what-is-the-best-regular-expression-to-check-if-a-string-is-a-valid-url – Jimmy C May 02 '13 at 11:57
  • @DiederikEEn can u show it pls – Techy May 02 '13 at 11:58
  • Have a look at this question too [http://stackoverflow.com/questions/833469/regular-expression-for-url](http://stackoverflow.com/questions/833469/regular-expression-for-url) – Maria Ioannidou May 02 '13 at 12:00
  • and this one: http://stackoverflow.com/questions/161738/what-is-the-best-regular-expression-to-check-if-a-string-is-a-valid-url?lq=1 – fotanus May 02 '13 at 12:11
  • @fotanus My need is to check for atleast one dot in the content and some text after dot. – Techy May 02 '13 at 12:12

3 Answers3

1

Your current regex doesn't work because it expects exactly three characters before the .: first a character that matches [a-zA-Z0-9], then a character that matches [a-zA-Z0-9-], then a character that matches [a-zA-Z0-9]. Your regex only permits a single . anywhere in the input.

For variable length strings you need to use + or * or the {} syntax like you do for the last part of your regex.

To keep essentially the same validation that you seem to be shooting for but have it work for varying lengths, try:

/^[A-Z\d-]+(\.[A-Z\d-]+)*\.[A-Z]{2,}$/i

That is:

[A-Z\d-]+ match one or more letters, digits or hyphens, followed by

(\.[A-Z\d-]+)* zero or more instances of a dot followed by one or more of those characters, followed by

\.[A-Z]{2,} a final dot with two or more of A-Z.

Note that you can get by with just the upper-case A-Z range if you add the i flag to the regex to make it case-insensitive.

nnnnnn
  • 147,572
  • 30
  • 200
  • 241
1

Your original pattern wants to only allow urls that has the first part not start or end with a dash. In case that is important, I've made a pattern which does that for you.

/^(?:(?!-)[a-z\d-]+[^-]\.)+[a-z]{2,6}$/i

This fixes the same problems that nnnnn solved in his answer, but it also doesn't allow any part of the url to start or end with a hyphen.

I can also recommend regexpal to test matching in real time. It uses javascript style regex matching.

Put the regex into the top field and the test data in the bottom. You will need to remove the slashes encapsulating it. Check the box for "Case insensitive (i)" and "^$ match at line breaks (m)"

Some test data:

google.com
yahoo-.com
www.YAHOO.co.uk
-yahoo.co.uk
http://www.regular-expressions.info/
hey.subdomain.sub.sub.domain.com
co.uk

Now, the difference between using ^$ or not will prove itself. You will see that some urls are not matching, even though they are valid. The multiline (m) flag allows you to think of each line in the input as a separate string. The regex I gave you will only match if the entire string matches, but some urls are valid even so. Case insensitive (i) equals the "i" at the end of the regex.

Try to remove the anchors for start of line (^) and end of line ($), and see how that matches.

I'm not sure how you are running your matches, but this is worth considering.

melwil
  • 2,547
  • 1
  • 19
  • 34
0

Try this:

(?:[-A-Za-z0-9]+\.)+[A-Za-z]{2,6}
CloudyMarble
  • 36,908
  • 70
  • 97
  • 130
webmonkey
  • 1,083
  • 1
  • 15
  • 33
  • Sure it does. Please add `^$` to the code: `^(?:[-A-Za-z0-9]+\.)+[A-Za-z]{2,6}$`, so the whole line is recognized. – webmonkey May 02 '13 at 12:08
  • ok.One more issue is there.When i type full word without any dots,eg:stackoverflow it does not pick error.My requirement is it should check for atleast one dot and a string after dot – Techy May 02 '13 at 12:11
  • It does. It checks for it, and if you don't have a dot followed by at least 2 chars, it is not valid. Look: http://regexpal.com/?flags=gm&regex=%5E(%3F%3A%5B-A-Za-z0-9%5D%2B%5C.)%2B%5BA-Za-z%5D%7B2%2C6%7D%24&input=google.com%0Astackoverflow%0Aexample.com%0A – webmonkey May 02 '13 at 12:15
  • My requirement is when I type stackoverflow ,it should point error,If I type stackoverflow.com,It should pass or if its google.co.in,it should pass.Hope u understand – Techy May 02 '13 at 12:15
  • Yes, that is exactly that. Have a look at the regexpal.com-link I posted in my previous comment. – webmonkey May 02 '13 at 12:17
  • .when i check i didnt get it correctly.I am umaware of using regexpal.com – Techy May 02 '13 at 12:23
  • No problem. You have to set the `^$ match at line breaks (m)`-checkbox at the top of the page. – webmonkey May 02 '13 at 12:24
  • You can see the regex from this answer working in the context of a JS function at this fiddle: http://jsfiddle.net/6yJQy/1/ – nnnnnn May 02 '13 at 12:28