 |
Cleaning a string using a regular expression ready for placing in a URL
Paul Hayman (546 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#}
|
 |
Regex match html content without screwing up the tags
Paul Hayman (1668 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
|
 |
Using Regex Look Arounds
Paul Hayman (2375 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 named match groups in expressions
Paul Hayman (1540 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
|
 |
Using Regex.Replace
Paul Hayman (2674 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 Regular Expressions to validate a filename in a FileUpload control
Paul Hayman (16212 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#{
|
 |
Highlighting keywords in text using Regex.Replace (Perfect for SEO)
Paul Hayman (8495 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
|