Blog posts about regular expressions

  • 3 must have tools for .net developers

    April 30, 2011 | Tags: ASP.NET, C#, Regular expressions


    ReSharper from Jetbrain is one of the most useful development tools I've ever come across. As a plugin to Visual Studio it offers code analysis - and suggesting changes to improve the code quality, smart refactoring and much more. You'll find a full list of features here.

    Download ReSharper (30 day trial)


    FxCop from Microsoft is also a great piece of software to increase the build quality of your project. By analyzing your assemblies (DLLs) against a best-practice set of rules it can pin-point problems in your code and offer suggestions.

    The installation is a bit daunting since it require you to download the complete Windows SDK. But it's worth it!

    Information on how to download FxCop


    Constructing regular expressions can sometimes be really tricky. Wouldn't it be great to have a tool that helps you create a regular expression from a given string and then tweak it? Well that's what Regulazy does.

    Download Regulazy

  • Remove extra white-spaces within a string

    March 07, 2011 | Tags: C#, Regular expressions, Snippet
    Sometimes the trim function isn't enough. Say that you have a string looking like:
    "    this    is  a test     string   "

    Using Trim() would get rid of the extra white spaces in the beginning and the end, but it would still leave you with:
    "this    is  a test     string"

    So how to handle all extra white spaces, even those within the string? We'll lets use some regular expression for that:
    using System.Text.RegularExpressions;

    // Remove extra white-spaces within a string
    text = Regex.Replace(text, @"\s+", " ");

  • Extract image tags from HTML in C#

    February 25, 2010 | Tags: C#, Regular expressions, Nils, Snippet
    A pretty little C# function that returns a list of strings containing all image tags extracted from a HTML string.
    private List<string> GetImagesInHTMLString(string htmlString) {
    List<string> images = new List<string>();
    string pattern = @"<(img)\b[^>]*>";

    Regex rgx = new Regex(pattern, RegexOptions.IgnoreCase);
    MatchCollection matches = rgx.Matches(htmlString);

    for(int i=0, l=matches.Count; i<l; i++) {

    return images;

  • Remove in javascript string

    November 02, 2009 | Tags: Javascript, Regular expressions, Nils, Snippet
    Yet another extension to the String object in Javascript submitted by Nils Forsman. This one adds the function of removing a part of a string, given a startposition and the length of the part to remove:
    //A - Startindex
    //B - Length
    String.prototype.remove = function(A, B) {
    var s = '';
    if (A > 0)
    s = this.substring(0, A);
    if (A + B < this.length)
    s += this.substring(A + B, this.length);
    return s;
    And some test code:
    var test = "test string test ";
    var result = test.remove(12,3);

  • Extend String with trim functions

    November 02, 2009 | Tags: Javascript, Regular expressions, Nils, Snippet
    This is a neat Javascript extension to the String object contributed by Nils Forsman.

    Here's the code that adds trim, ltrim and rtrim to the String object:
    String.prototype.trim = function() {
    return this.replace(/(^\s*)|(\s*$)/g, '');

    String.prototype.ltrim = function() {
    return this.replace(/^\s*/g, '');

    String.prototype.rtrim = function() {
    return this.replace(/\s*$/g, '');
    And a little code snippet to test the above functions:
    var testString = " test ";
    testString = testString .trim();
    alert("#" + testString + "#");