RegEx Problem: Valid URL does not match, but should

I posted a similar question earlier, but I now realize I should have been more thorough.

I've tested a number of the URL/URI expressions listed on regexlib.com, but I can't get any of them to work as desired:

msn.com msn-msn.net yahoo.c!om http://www.yahoo.com msn msn test ! number 1

Here is how I desire them to act:

msn.com (match) msn-msn.net (match) yahoo.c!om (fail) http://www.yahoo.com (match) msn msn (fail) test ! number 1 (fail)

I'm using the tester here: http://regexlib.com/RETester.aspx before testing in my own app (C#, .NET 4.0)

The expression that is closest is this, but it doesn't match the http://www.yahoo.com one:

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

Any help is appreciated. Additionally, somebody should come up with a more human-readable equivalent to RegEx...this stuff is a nightmare.

Thanks, Beems

--------------Solutions-------------

If you can't guarantee that the URL-esque pattern you're trying to match has a scheme/protocol, then the safest thing to do is match against top-level domains:

^(https?://)[^/]*.([possibly|really|long|list|of|valid|top|level|domains][2])

From your post it's evidently not necessary to go into the path, hash, or querystring parts of a URL, so that's it!

This one appears to work as desired:

[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(/\S*)?

Can anyone see any issues with this in regards to my original query? I don't need to validate whether the TLD is proper, so this isn't really an issue.

Agree with kojiro

But this does match your tests

http://www.rubular.com/r/gUb4U6Pzux

Category:c# Time:2011-03-01 Views:1
Tags: c# regex

Related post

  • regex javascript supply valid url image 2012-01-21

    I need a regex for valid url image, example: protocol: http,https,ftp estension: jpg,png,gif,bmp Can you help me please? --------------Solutions------------- Maybe something like that: "^(https?|ftp)://.*(jpeg|png|gif|bmp)" Try this, /^((https?|ftp):

  • Need a regex to validating a Url and support %20 and () 2010-01-14

    I'm currently using the following regular expression to validation URLs: ^(?#Protocol)(?:(?:ht|f)tp(?:s?)\:\/\/|~\/|\/)?(?#Username:Password)(?:\w+:\[email protected])? (?#Subdomains)(?:(?:[-\w]+\.)+(?#TopLevel Domains)(?:com|org|net|gov|mil|biz|edu|info|mobi|name

  • PHP regex for validating a URL 2010-03-05

    I'm looking for a decent regex to match a URL (a full URL with scheme, domain, path etc.) I would normally use filter_var but I can't in this case as I have to support PHP<5.2! I've searched the web but can't find anything that I'm confident will

  • Regex in URL Rewriting to match Querystring Parameter Values in any order? 2010-04-28

    Many URL rewriting utilities allow Regex matching. I need some URLs to be matched against a couple of main querystring parmeter values no matter what order they appear in. For example let's consider an URL having two key parameters ID= and Lang= in n

  • Regex to check if valid URL that ends in .jpg, .png, or .gif 2008-10-04

    I would like users to submit a URL that is valid but also is an image, ending with .jpg, .png, or .gif. --------------Solutions------------- (?:([^:/?#]+):)?(?://([^/?#]*))?([^?#]*\.(?:jpg|gif|png))(?:\?([^#]*))?(?:#(.*))? That's a (slightly modified

  • Regex to check a Valid URL 2012-03-16

    Problem: I need a Regex which would check a given author URL is valid or not. Requirement : Author URL is basically a URL from social networking sites/blogs etc having author id (profile id) For eg . www.facebook.com/RyanMathews www.mouthshut.com/zob

  • Regex IP for URL problem 2010-08-21

    const string strRegex = @"^(http|https|ftp)\://[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}" "(:[a-zA-Z0-9]*)?/?([a-zA-Z0-9\-\._\?\,\'/\\\+&amp;%\$#\=~])*[^\.\,\)\(\s]$"; Regex rgxUrl = new Regex(strRegex, RegexOptions.Compiled |RegexOptions.IgnoreCase); If m

  • Regex problem with match method 2011-02-17

    I want validate many mails ( one or more) with regex expression but this attribute don´t belongs to any model. So I wrote a method: def emails_are_valid?(emails) #regex with validation regex = Regexp.new("^(\s*[a-zA-Z0-9\._%-][email protected][a-zA-Z0-9\.-]+\.[a-zA

  • What is the best regular expression to check if a string is a valid URL? 2008-10-02

    How can I check if a given string is a valid URL address? My knowledge of regular expressions is basic and doesn't allow me to choose from the hundreds of regular expressions I've already seen on the web. --------------Solutions------------- I wrote

  • RegEx : replace all Url-s that are not anchored 2011-08-31

    I'm trying to replace Urls contained inside a HTML code block the users post into an old web-app with proper anchors (<A>) for those Urls. The problem is that Urls can be already 'anchored', that is contained in <A> elements. Those Url sh

  • Route-problem regarding Url-encoded Umlauts (using the Zend-framework) 2008-09-24

    Today I stumbled about a Problem which seems to be a bug in the Zend-Framework. Given the following route: <test> <route>citytest/:city</route> <defaults> <controller>result</controller> <action>test</acti

  • What's wrong with this RegEx for validating emails? 2009-07-02

    Here's a regex for validating emails - \[email protected]\S+\.\S+, I didn't write it. I'm new to Regular Expressions and do not understand them all that well. I have a couple of questions: What's wrong with the above RegEx? What's a good RegEx for validating email

  • regex to remove URL from text 2009-12-18

    I want to remove all occurrences of URL [full path, query string] from the text in Python. Any suggestions on how to do this? I am new to regex! http://example.com/url/?x=data This whole URL should be removed! Thanks --------------Solutions----------

  • Regex to validate URL - Not checking for HTTP? 2010-02-14

    I know there are tonns of questions on here to validate a web address with something like this /^[a-zA-Z]+[:\/\/]+[A-Za-z0-9\-_]+\\.+[A-Za-z0-9\.\/%&=\?\-_]+$/i The only problem is, not everybody uses the http:// or whatever comes before so i wan

  • How to use regex to replace URLs with an HTML link in Qt? 2010-07-23

    How do I use QString::replace to detect URLs in a string and replace them with an HTML link, like so... [...].replace(QRegExp("???"), "<a href=\"\\1\">\\1</a>") What should the argument to QRegExp be? The end of a URL should be denoted by

  • issues with my regex to detect urls in a string? 2010-12-23

    Greetings all. I am using the following regex to detect urls in a string and wrap them inside the < a > tag public static String detectUrls(String text) { String newText = text .replaceAll("(?:https?|ftps?|http?)://[\\w/%.-?&=]+", "<a hr

  • What is the RFC complicant and working regular expression to check if a string is a valid URL 2011-01-17

    There is question by the almost the same name already: What is the best regular expression to check if a string is a valid URL I don't understand this stackoverflow. It seems like I need reputation to comment an answer. As I don't have it, I don't kn

  • Regex to find URL within text and make them as link 2011-03-10

    I am trying to output a string as html which has links in it. I want to make these links actual links. My test string ="https://www.google.com http://yahoo.com www.msn.com www.google.com" My code: Dim oRegEx As New Regex("((https?:\/\/|www\.)([-\w\.]

  • Regex to catch url in a string 2011-04-04

    Possible Duplicates: Regex to match URL What is the best regular expression to check if a string is a valid URL I want to catch urls in strings if any. What is the best regex for this? Urls may or not be abolute. http://www.blabla.com http://blabla.c

Copyright (C) pcaskme.com, All Rights Reserved.

processed in 0.511 (s). 13 q(s)