URL Regular Expression
Paul Hayman (124681 views)
URL Regular Expression
#c#string pattern = @"((https?|ftp|gopher|telnet|file|notes|ms-help):((//)|(\\\\))+[\w\d:#@%/;$()~_?\+-=\\\.&]*)"
#c#
|
Using Regular Expressions to validate a filename in a FileUpload control
Paul Hayman (57683 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#{
|
IP Address regular expression
Paul Hayman (53963 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#
|
IsGuid() (Regular Expression Guid Match)
Paul Hayman (47723 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
|
Highlighting keywords in text using Regex.Replace (Perfect for SEO)
Paul Hayman (17479 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/Services/Search-Engine-Optimisation
Example
The f
|
Getting the Virtual Path of a Request in c#
Paul Hayman (13158 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;
#
|
Using Regex Look Arounds
Paul Hayman (10653 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
|
Using Regex.Replace
Paul Hayman (9214 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
|
ISBN Number regular expression
Paul Hayman (9117 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
|
Regex match html content without screwing up the tags
Paul Hayman (9084 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 (3961 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 (3850 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
|
Cleaning a string using a regular expression ready for placing in a URL
Paul Hayman (3054 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#}
|
Mail and News Regular Expression
Paul Hayman (2765 views)
Mail and News Regular Expression
#c#string pattern = @"(?<ignore>(^|\s|\W)[\W*])?(?<uri>((mailto|news){1}:[^(//\s,)][\w\d:#@%/;$()~_?\+-=\\\.&]+))"
#c#
|
Regex for a HTML tag
Paul Hayman (2486 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.
|
Non Capturing Groups
Paul Hayman (2127 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
|