General Articles

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,227 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 - 18,232 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,881 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,315 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,861 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 - 71,052 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 - 27,694 views