Regular Expressions Library :: Internet Related

This section contains a number of regular expressions related to the internet. This includes URLs, email addresses and working with HTML. To match a string that matches one of these expressions as a whole rather than in part, put a ^ before the expression and a $ after it. For example, \d+ becomes ^\d+$.

URLs (Simple)

Description: This is the quick and easy way to match a simple URL without a query string or any authentication data. It is tight enough to be useful for most URL validation tasks, but will match things thare do not strictly meet the specification for a URL. Note that, in the expressions below, slashes are escaped (a backslash is put before them) as slashes are often used to delimit regular expressions. These can be simplified where a case insensitivity flag is available.
Perl Compatible Form: [A-Za-z]\w+:\/\/[\w\-.]+(:\d+)?\/[\w.\-~\/%]+
POSIX Form: [A-Za-z][A-Za-z0-9]+:\/\/[[A-Za-z0-9]\-.]+(:[0-9]+)?\/[[A-Za-z0-9].\-~\/%]+

URL (Specific Protocols)

Description: This allows the protocol to be limited to one of a pre-defined set. Setting the case -insensitive flag is recommended, as URLs are not case sensitive. In the example below, http, https and ftp are allowed. Any of the following examples can be modified in this way to restrict the available protocols.
Perl Compatible Form: (http|https|ftp):\/\/[\w\-.]+(:\d+)?\/[\w.\-~\/%]+
POSIX Form: (http|https|ftp):\/\/[[A-Za-z0-9]\-.]+(:[0-9]+)\/[[A-Za-z0-9].\-~\/%]+

URL With Optional Query String

Description: This adds an extra bit to the end of the expression to match URLs with query strings.
Perl Compatible Form: [A-Za-z]\w+:\/\/[\w\-.]+(:\d+)?\/[\w.\-~\/%]+(\?[^\s#]*|\?)
POSIX Form: [A-Za-z][A-Za-z0-9]+:\/\/[[A-Za-z0-9]\-.]+(:[0-9]+)?\/[[A-Za-z0-9].\-~\/%]+(\?[^ \t\n\r#]*|\?)

URL With Optional Fragment (Hyper-jump)

Description: This adds another extra bit to the end of the expression to identify fragments (in HTTP, these are known as hyper-jumps).
Perl Compatible Form: [A-Za-z]\w+:\/\/[\w\-.]+(:\d+)?\/[\w.\-~\/%]+(#\w+)?
POSIX Form: [A-Za-z][A-Za-z0-9]+:\/\/[[A-Za-z0-9]\-.]+(:[0-9]+)?\/[[A-Za-z0-9].\-~\/%]+(#[A-Za-z0-9_]+)?

Email Address

Description: This matches an email address. Just because an email address matches does not mean it is valid and that email sent there will get to a recipient - it simply means that it is in the right form to be an email address that might work.
Perl Compatible Form: [\w\-.]+@[\w\-.]+\.[A-Za-z]{2,4}
POSIX Form: [A-Za-z0-9\-.]+@[A-Za-z0-9\-.]+\.[A-Za-z][A-Za-z][A-Za-z]?[A-Za-z]?

Extracting Links From HTML Documents

Description: This expression matches and captures links that are contained within HTML documents. HTML itself is not a regular language, and this regular expression is not parsing the HTML document, which is what would be needed to get perfect results. All the same, it should cover most bases. Do set the case sensitive flag.
Perl Compatible Form: <a\s+[^>]*?href\s*=\s*"?([^"\s]+)
POSIX Form: <a[ \t\r\n]+[^>]*href[ \t\r\n]*=[ \t\r\n]*"?([^" \t\r\n]+)

[usercomment=s:3:16301]

 
Printer friendly version of the RegexLibraryNet page


Sponsored links

ASP.NET 3.5 Hosting on Windows 2008!
ASP.NET 3.5/2.0 Hosting on Windows 2008 & 2003! AJAX, LINQ, & Silverlight Ready! 3 Mo. Free!!
Build IT Knowledge with Current & Trusted Content
Helps Employees Develop & Hone New Technical Programming Skills. Sign Up & Get Full Access.
Check Out IT Certification Preparation Materials
Sign Up With SkillSoft & Get Access to Training Materials for Over 50 Professional Certifications.
SFTP components for .NET
Add complete SSH and SFTP support to your .NET framework application
Experience Adobe? FLASH MEDIA SERVER 3
Introducing the media solution for total action without interruption. TRY IT NOW FOR FREE!

Advertisement



Free Magazine

Free Magazines
eWeek The essential technology information source for builders of e-business.... subscribe now

Newsletter | Submit Content | About | Advertising | Awards | Contact Us | Link to us |
© 1996-2008 Community Networks Ltd All rights reserved. Reproduction in whole or in part, in any form or medium without express written permission is prohibited. Violators of this policy may be subject to legal action. Please read Terms Of Use and Privacy Statement for more information. Development by Synchron Data - .NET development.