 |
Using Regular Expressions to validate a filename in a FileUpload control
Paul Hayman (18055 views)
Using Regular Expressions to validate a filename in a FileUpload control
Here's a little code snippet I use to ensure that the file that has been uploaded is of type JPG, JPEG, PNG or GIF
#c#// check anything has been uploaded
#c#if (ThumbnailImageUpload.PostedFile.ContentLength > 0)
#c#{
|
 |
URL Regular Expression
Paul Hayman (16595 views)
URL Regular Expression
#c#string pattern = @"((https?|ftp|gopher|telnet|file|notes|ms-help):((//)|(\\\\))+[\w\d:#@%/;$()~_?\+-=\\\.&]*)"
#c#
|
 |
IsGuid() (Regular Expression Guid Match)
Paul Hayman (11942 views)
IsGuid() (Regular Expression Guid Match)
A regular expression for validating a string as being a Guid is..
#c#@"^(\{{0,1}([0-9a-fA-F]){8}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){12}\}{0,1})$"
#c#
Example usage
Below is a function I try to keep handy which tests
|
 |
IP Address regular expression
Paul Hayman (11689 views)
IP Address regular expression
#c#string pattern = @"\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b"
#c#
|
 |
Highlighting keywords in text using Regex.Replace (Perfect for SEO)
Paul Hayman (9426 views)
Highlighting keywords in text using Regex.Replace (Perfect for SEO)
Why
I needed to take some text and bold certain keywords before returning the data to the web browser to enhance my {Search Engine Optimization}http://www.kwiboo.com/SearchEngineOptimisation.aspx
Example
The followi
|
 |
ISBN Number regular expression
Paul Hayman (2980 views)
ISBN Number regular expression
#c#string pattern = @"ISBN\x20(?=.{13}$)\d{1,5}([- ])\d{1,7}\1\d{1,6}\1(\d|X)$";
Examples
#c#// Valid
#c#Console.WriteLine(Regex.IsMatch(@"ISBN 0 93028 923 4", @"ISBN\x20(?=.{13}$)\d{1,5}([- ])\d{1,7}\1\d{1,6}\1(\d|X)$"));
#c#
#c#// Valid
#c#Console.Wr
|
 |
Using Regex.Replace
Paul Hayman (2966 views)
Using Regex.Replace
Say you want to get the username from a fully qualified username such as '''MyDomain\AUser'''
Most developers would turn to SubString and LastIndexOf functions .. for example
#c#string userName = @"MyDomain\AUser";
#c#string result = userName.SubString(userName.LastInde
|
 |
Using Regex Look Arounds
Paul Hayman (2686 views)
Using Regex Look Arounds
.net supports four types of "Look Around". These can be used to make sure that patterns do or do not appear before or after whatever it is you're matching on. Consider the following text:
#c#"my car will be off the road for 2 days. it's reg num is ab123abc"
If I w
|
 |
Getting the Virtual Path of a Request in c#
Paul Hayman (2558 views)
Getting the Virtual Path of a Request in c#
This simple bit of code will get you the '''Virtual Path''' of your current request.
#c#public static string GetVirtualPath(string url)
#c#{
#c# if (HttpContext.Current.Request.ApplicationPath == "/")
#c# {
#c# return "~" + url;
#
|
 |
Regex match html content without screwing up the tags
Paul Hayman (2438 views)
Regex match html content without screwing up the tags
When needing to highlight words in a string containing HTML we found we soon ran into problems when the word we were searching for appeared in the middle of a tag..
Imagine the example:
#v#<a href="geekzilla.aspx">you searched for geek
|
 |
Email Address Regular Expression
Paul Hayman (2163 views)
Email Address Regular Expression
#c#string pattern = @"([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)"
#c#
|
 |
Using named match groups in expressions
Paul Hayman (1687 views)
Using named match groups in expressions
The following code will return the value '''user'''
#c#Console.WriteLine(Regex.Match(@"test\user", @".*\\(.*?)$").Groups[1]);
Sometimes it makes life easier to refer to the group by name rather than its position (or GroupNum). To do this we need to
|
 |
Mail and News Regular Expression
Paul Hayman (1239 views)
Mail and News Regular Expression
#c#string pattern = @"(?<ignore>(^|\s|\W)[\W*])?(?<uri>((mailto|news){1}:[^(//\s,)][\w\d:#@%/;$()~_?\+-=\\\.&]+))"
#c#
|
 |
Non Capturing Groups
Paul Hayman (944 views)
Non Capturing Groups
Expressions can contain a lot of groups which you weren't interested in matching on. It is possible to tell .net not to capture these by placing a '''?:''' at the beginning of the group. Apparently limiting the number of capturing groups has a '''positive increase on perfor
|
 |
Regex for a HTML tag
Paul Hayman (929 views)
Regex for a HTML tag
#c#<.[^>]*>
This can proove invaluable when you're wanting to replace keywords in HTML without spoiling the tags themselves. Particularly useful in SEO.
|
 |
Cleaning a string using a regular expression ready for placing in a URL
Paul Hayman (752 views)
Cleaning a string using a regular expression ready for placing in a URL
#c#public static string CleanForUrl(string text)
#c#{
#c# return Regex.Replace(text, @"[^a-z^A-Z^0-9^-]", "-");
#c#}
|