Articles tagged under regex

Cleaning a string using a regular expression ready for placing in a URL

Cleaning a string using a regular expression ready for placing in a URL public static string CleanForUrl(string text) {     return Regex.Replace(text, @"[^a-z^A-Z^0-9^-]", "-"); }

Paul Hayman - 4,133 views

Regex for a HTML tag

Regex for a HTML tag <.[^>]*> This can proove invaluable when you're wanting to replace keywords in HTML without spoiling the tags themselves. Particularly useful in SEO.

Paul Hayman - 2,849 views

Regex match html content without screwing up the tags

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: <a href="geekzilla.aspx">you

Paul Hayman - 16,545 views

Getting the Virtual Path of a Request in c#

Getting the Virtual Path of a Request in c# This simple bit of code will get you the Virtual Path of your current request. public static string GetVirtualPath(string url) {     if (HttpContext.Current.Request.Application

Paul Hayman - 18,729 views

Non Capturing Groups

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 performance.

Paul Hayman - 2,449 views

Using Regex Look Arounds

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: "my car will be off the road for 2 days. it's reg num is ab123abc"

Paul Hayman - 13,786 views

ISBN Number regular expression

ISBN Number regular expression string pattern = @"ISBN\x20(?=.{13}$)\d{1,5}([- ])\d{1,7}\1\d{1,6}\1(\d|X)$"; Examples // Valid 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)$

Paul Hayman - 11,349 views

IP Address regular expression

IP Address regular expression 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"  

Paul Hayman - 81,915 views

Using named match groups in expressions

Using named match groups in expressions The following code will return the value user 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 ne

Paul Hayman - 4,226 views

Using Regex.Replace

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 string userName = @"MyDomain\AUser"; string result = userNam

Paul Hayman - 11,766 views

Using Regular Expressions to validate a filename in a FileUpload control

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 // check anything has been uploaded if (ThumbnailImageUpload.Poste

Paul Hayman - 69,961 views

Email Address Regular Expression

Email Address Regular Expression 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})(\]?)"  

Paul Hayman - 4,474 views

Mail and News Regular Expression

Mail and News Regular Expression string pattern = @"(?<ignore>(^|\s|\W)[\W*])?(?<uri>((mailto|news){1}:[^(//\s,)][\w\d:#@%/;$()~_?\+-=\\\.&]+))"  

Paul Hayman - 3,492 views

URL Regular Expression

URL Regular Expression string pattern = @"((https?|ftp|gopher|telnet|file|notes|ms-help):((//)|(\\\\))+[\w\d:#@%/;$()~_?\+-=\\\.&]*)"  

Paul Hayman - 152,196 views

Highlighting keywords in text using Regex.Replace (Perfect for SEO)

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 Example The following example shows how I achieved this althou

Paul Hayman - 26,772 views

IsGuid() (Regular Expression Guid Match)

IsGuid() (Regular Expression Guid Match) A regular expression for validating a string as being a Guid is.. @"^(\{{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})$" Example usage Below is a function I try to keep handy which t

Paul Hayman - 91,584 views