«

A simple Regular Expression for matching urls in any text.

A co-worker was looking to find urls in a bunch of text files, and had limited success with the examples at the Regular Expression Library. Here’s a quick little expression that I whipped up for him. I’ve also added it to the library for the 6 billion or so people in the world who aren’t subscribed to my feed.

[a-zA-Z]{3,}://[a-zA-Z0-9\.]+/*[a-zA-Z0-9/\\%_.]*\?*[a-zA-Z0-9/\\%_.=&]*

It doesn’t include every possible pattern, but it works flawlessly for the following sample text:

this is my url http://test/test.jpg?test=1&foo=bar EndProject ftp://products/ and how about http://someserver or http://www.msn.com/default.aspx#test ok? ok http://www.nothere.com/index_file.html ha ha ha <a href=”http://dev1″>Dev1</a>

I’ve only tested it using .Net, but it will probably work with any regular expression engine.