My name is Edward Tanguay. I'm an American software and web developer living and working in Berlin, Germany.
C# CODE EXAMPLE created on Tuesday, June 01, 2010 permalink
How to strip off e.g. "note:" and "firstName: " from the left of a string using regex
This codes shows how you can use regular expressions to strip off a "label + colon" from a line e.g. if you are parsing through lines labels with codes on the left.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace TestRegex8822
{
    class Program
    {
        static void Main(string[] args)
        {
            List<string> lines = new List<string>();
            lines.Add("note: this is a note");
            lines.Add("test:    just a test");
            lines.Add("test:tttjust a test");
            lines.Add("firstName: Jim"); //"firstName" IS a label because it does NOT contain a space
            lines.Add("first name: Jim"); //"first name" is not a label because it contains a space
            lines.Add("description: this is the first description");
            lines.Add("description:this is the second description"); //no space after colon
            lines.Add("this is a line with no label");
            lines.Add("she said to him: follow me");

            foreach (var line in lines)
            {
                LabelLinePair llp = line.GetLabelLinePair();
                Console.WriteLine(llp.Label);
                Console.WriteLine(llp.Line);
                Console.WriteLine("--");
            }
            Console.ReadLine();
        }
    }

    public static class StringHelpers
    {
        public static LabelLinePair GetLabelLinePair(this string line)
        {
            Regex regex = new Regex(@"(?<label>.+):s*(?<text>.+)");
            Match match = regex.Match(line);
            LabelLinePair llp = new LabelLinePair();
            llp.Label = match.Groups["label"].ToString();
            llp.Line = match.Groups["text"].ToString();

            if (llp.Label.IsNullOrEmpty() || llp.Label.Contains(" "))
            {
                llp.Label = "";
                llp.Line = line;
            }

            return llp;
        }

        public static bool IsNullOrEmpty(this string line)
        {
            return String.IsNullOrEmpty(line);
        }
    }

    public class LabelLinePair
    {
        public string Label { get; set; }
        public string Line { get; set; }
    }
}
need markup?