20

I have written regex to validate URL which could be either like

google.com

www.google.com

http://www.google.com

https://www.google.com

I have used

Regex urlRx = new Regex(@"^(http|ftp|https|www)://([\w+?\.\w+])+([a-zA-Z0-9\~\!\@\#\$\%\^\&\*\(\)_\-\=\+\\\/\?\.\:\;\'\,]*)?$", RegexOptions.IgnoreCase);

It works for http & https.It is not working for google./com & www.google.com.

Please help me to solve this.

Thanks

4
  • 4
    Here's a nice page with the comparison of different regexes for parsing urls: mathiasbynens.be/demo/url-regex The best one is: _^(?:(?:https?|ftp)://)(?:\S+(?::\S*)?@)?(?:(?!10(?:\.\d{1,3}){3})(?!127(?:\.\d{1,3}){3})(?!169\.254(?:\.\d{1,3}){2})(?!192\.168(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\x{00a1}-\x{ffff}0-9]+-?)*[a-z\x{00a1}-\x{ffff}0-9]+)(?:\.(?:[a-z\x{00a1}-\x{ffff}0-9]+-?)*[a-z\x{00a1}-\x{ffff}0-9]+)*(?:\.(?:[a-z\x{00a1}-\x{ffff}]{ Commented Feb 15, 2012 at 6:45
  • I used this one (without ftp) |^http(s)?://[a-z0-9-]+(.[a-z0-9-]+)*(:[0-9]+)?(/.*)?$|i Commented Feb 15, 2012 at 6:48
  • does it work for www.google.com? Commented Feb 15, 2012 at 6:54
  • Be warned, for simple cases, this is simple, for complicated cases (e.g. with a query string or special characters in the url), it's not simple... Commented Feb 15, 2012 at 8:08

4 Answers 4

63

no need for a regex IMHO - try

Uri.IsWellFormedUriString(YourURLString, UriKind.RelativeOrAbsolute)

See MSDN

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

9 Comments

@PrateekSaluja I am not sure that I understand - YourURLString is just normal string... or do have a normal text with some links somewhere in the middle of it ?
Please test this message. "Hello how are you? www.google.com"
Be careful using this as the only way to validate a URL, as it allows for URIs such as javascript:alert('Invitation to hack me');
@Yahia - Your answer allows for non-web URLs (eg file paths) and I think the OP is looking to ensure the URLs are valid http/http URLs.
This doesn't work if you need client side validation. Granted, this isn't what the OP asked... just making a comment.
|
6

Put the protocol section in an optional group i.e., ()?:

^((http|ftp|https|www)://)?([\w+?\.\w+])+([a-zA-Z0-9\~\!\@\#\$\%\^\&\*\(\)_\-\=\+\\\/\?\.\:\;\'\,]*)?$

1 Comment

System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex(@"^((http|ftp|https|www)://)?([\w+?\.\w+])+([a-zA-Z0-9\~\!\@\#\$\%\^\&\*\(\)_\-\=\+\\\/\?\.\:\;\'\,]*)?$");
3

I got this code from jquery.validation (i made some edits)

bool isValid = (Regex.IsMatch(value, @"(((([a-z]|\d|-|.||~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'()*+,;=]|:)*@)?(((\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5]).(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5]).(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5]).(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5]))|((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|.||~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))).)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))).?)(:\d*)?)(/((([a-z]|\d|-|.||~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'()*+,;=]|:|@)+(/(([a-z]|\d|-|.||~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'()*+,;=]|:|@)))?)?(\?((([a-z]|\d|-|.||~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'()*+,;=]|:|@)|[\uE000-\uF8FF]|/|\?)*)?(#((([a-z]|\d|-|.||~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'()*+,;=]|:|@)|/|\?)*)?$"));

I was searching for almost a whole day and this is the best I found.. These are just the few valid samples:

  1. example.com
  2. example.com.ph
  3. www.example.com
  4. http://example.com
  5. https://example.com
  6. http://www.example.com
  7. ftp://example.com
  8. example.com/doc
  9. www.example.com/doc
  10. http://example.com/doc
  11. http://example.com/questions/12576252/convert-javascript-regex-to-c-sharp-regex-for-email-validation
  12. http://www.example.com/wpstyle/?p=364
  13. http://userid:[email protected]:8080
  14. https://www.example.com/foo/?bar=baz&inga=42&quux
  15. LDAP://ad1.cc.uq.edu.ph

1 Comment

I tried this one but it returned a false positive for a phone number like this: (937) 555-1212
0
^(http|http(s)?:\/\/)?([\w-]+\.)+[\w-]+[.com|.in|.org]+

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.