-2

I'm looking for a suitable regex for hyperlinks.

I found a link to this website: https://mathiasbynens.be/demo/url-regex, which has several choices. (There are too many to list here.)

However, these do not seem to be compatible with .NET's Regex syntax. I tried using the "cowboy" pattern vs several hyperlinks, e.g. http://www.cnn.com, but no match results.

What syntax are these, and how do I get these to work with .NET? If these require manual tweaking, then just showing how to get the "cowboy" pattern to run will suffice.

4
  • Did you just copy and paste the whole pattern? It looks like the cowboy one uses tildes in the way that JavaScript or Ruby would use slashes /. That means you should only copy the pattern between the tildes and probably use RegexOptions.IgnoreCase.
    – kamilk
    Feb 13, 2016 at 13:00
  • The @cowboy regex works fine with .NET. Did you use '~ and ~iS? Feb 13, 2016 at 13:06
  • @kamilk: The (?i) is the case insensitive inline modifier. No need in RegexOptions.IgnoreCase. Feb 13, 2016 at 13:08
  • I reconsidered and this is what I think: I am not against helping to convert some code from a language to language, but it is not an effort to just copy/paste some code, see it does not work, and go ask for help on SO. No, that is still an invalid question. Please describe the issue you have had, what you did to fix that, and what exactly failed. SO is not a free code writing service. Feb 13, 2016 at 15:51

1 Answer 1

2

You need to only copy the pattern in between the tildes.

    static void Main(string[] args)
    {
        string pattern = "(?:\\b[a-z\\d.-]+://[^<>\\s]+|\\b(?:(?:(?:[^\\s!@#$%^&*()_=+[\\]{}\\|;:'\\\",.<>/?]+)\\.)+(?:ac|ad|aero|ae|af|ag|ai|al|am|an|ao|aq|arpa|ar|asia|as|at|au|aw|ax|az|ba|bb|bd|be|bf|bg|bh|biz|bi|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|cat|ca|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|coop|com|co|cr|cu|cv|cx|cy|cz|de|dj|dk|dm|do|dz|ec|edu|ee|eg|er|es|et|eu|fi|fj|fk|fm|fo|fr|ga|gb|gd|ge|gf|gg|gh|gi|gl|gm|gn|gov|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|im|info|int|in|io|iq|ir|is|it|je|jm|jobs|jo|jp|ke|kg|kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|me|mg|mh|mil|mk|ml|mm|mn|mobi|mo|mp|mq|mr|ms|mt|museum|mu|mv|mw|mx|my|mz|name|na|nc|net|ne|nf|ng|ni|nl|no|np|nr|nu|nz|om|org|pa|pe|pf|pg|ph|pk|pl|pm|pn|pro|pr|ps|pt|pw|py|qa|re|ro|rs|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|sk|sl|sm|sn|so|sr|st|su|sv|sy|sz|tc|td|tel|tf|tg|th|tj|tk|tl|tm|tn|to|tp|travel|tr|tt|tv|tw|tz|ua|ug|uk|um|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|xn--0zwm56d|xn--11b5bs3a9aj6g|xn--80akhbyknj4f|xn--9t4b11yi5a|xn--deba0ad|xn--g6w251d|xn--hgbk6aj7f53bba|xn--hlcj6aya9esc7a|xn--jxalpdlp|xn--kgbechtv|xn--zckzah|ye|yt|yu|za|zm|zw)|(?:(?:[0-9]|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])\\.){3}(?:[0-9]|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5]))(?:[;/][^#?<>\\s]*)?(?:\\?[^#<>\\s]*)?(?:#[^<>\\s]*)?(?!\\w))";
        string url = "http://www.stackoverflow.com";
        Console.WriteLine(Regex.IsMatch(url, pattern, RegexOptions.IgnoreCase));
    }
15
  • I already said that it works - why post answers? See WorksForMe. Feb 13, 2016 at 13:11
  • @WiktorStribiżew If it's an answer, it should be posted as an answer, not a comment. And there's a reasonable chance that kamilk started typing up an answer before you even posted your comment. From the question: "If these require manual tweaking, then just showing how to get the "cowboy" pattern to run will suffice."
    – user743382
    Feb 13, 2016 at 13:15
  • There is no issue in the question, and answers "it works" are not valid. Feb 13, 2016 at 13:16
  • @hvd: You are wrong assuming I want to answer an unclear, invalid question. Feb 13, 2016 at 13:17
  • 1
    @karmilk - appreciate the answer. You correctly deduced I was tripped by the tildes. Obviously the source pattern does not work as-is; not everybody knows these delimiters. Thanks for applying good common sense and deduction.
    – bright
    Feb 13, 2016 at 14:13

Not the answer you're looking for? Browse other questions tagged or ask your own question.