Common Regular Expression Patterns for C#
The following are code snippets for common regular expressions in C#.
If you have a regular expression that you think is common or a correction/improvement to one of mine, please submit it.
IP address pattern or expression
The expression:
^[\w!#$%&'*+\-/=?\^_`{|}~]+(\.[\w!#$%&'*+\-/=?\^_`{|}~]+)*@((([\-\w]+\.)+[a-zA-Z]{2,4}$)|(([0-9]{1,3}\.){3}[0-9]{1,3}))
In CSharp code:
String theIpAddressPattern = @"^[0-9]{1,3}\.){3}[0-9]{1,3}$";
Domain name pattern or expression
The expression:
^[\-\w]+\.)+[a-zA-Z]{2,4}$
In CSharp code:
String theDoainNamePattern = @"^[\-\w]+\.)+[a-zA-Z]{2,4}$";
Email address pattern or expression
The expression:
^[\w!#$%&'*+\-/=?\^_`{|}~]+(\.[\w!#$%&'*+\-/=?\^_`{|}~]+)*@((([\-\w]+\.)+[a-zA-Z]{2,4}$)|(([0-9]{1,3}\.){3}[0-9]{1,3}))
In CSharp code:
String theEmailPattern = @"^[\w!#$%&'*+\-/=?\^_`{|}~]+(\.[\w!#$%&'*+\-/=?\^_`{|}~]+)*" + "@" + @"((([\-\w]+\.)+[a-zA-Z]{2,4})|(([0-9]{1,3}\.){3}[0-9]{1,3}))$";
a
The URL Pattern
The expression:
((^http(s)*://(([\-\w]+\.)+[a-zA-Z]{2,4}.*)))$|(^ftp://([\w](:[\w]))*(([\-\w]+\.)+[a-zA-Z]{2,4}[/\w]*))$
In CSharp code:
String theURLPattern = @"((^http(s)*://(([\-\w]+\.)+[a-zA-Z]{2,4}.*)))$" + @"|(^ftp://([\w](:[\w]))*(([\-\w]+\.)+[a-zA-Z]{2,4}[/\w]*))$";
Copyright ® Rhyous.com – Linking to this page is allowed without permission and as many as ten lines of this page can be used along with this link. Any other use of this page is allowed only by permission of Rhyous.com.