What Regex can strip e.g. "note:" and "firstName: " from the left of a string?

Posted by Edward Tanguay on Stack Overflow See other posts from Stack Overflow or by Edward Tanguay
Published on 2010-06-01T16:07:14Z Indexed on 2010/06/01 16:13 UTC
Read the original article Hit count: 275

Filed under:
|
|

I need to strip the "label" off the front of strings, e.g.

note: this is a note

needs to return:

note

and

this is a note

I've produced the following code example but am having trouble with the regexes.

What code do I need in the two ???????? areas below so that I get the desired results shown in the comments?

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:\t\t\tjust a test");
            lines.Add("firstName: Jim"); //"firstName" IS a label because it does NOT contain a space
            lines.Add("She said this to him: follow me."); //this is NOT a label since there is a space before the colon
            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");

            foreach (var line in lines)
            {
                Console.WriteLine(StringHelpers.GetLabelFromLine(line));
                Console.WriteLine(StringHelpers.StripLabelFromLine(line));
                Console.WriteLine("--");
                //note
                //this is a note
                //--
                //test
                //just a test
                //--
                //test
                //just a test
                //--
                //firstName
                //Jim
                //--
                //
                //She said this to him: follow me.
                //--
                //description
                //this is the first description
                //--
                //description
                //this is the first description
                //--
                //
                //this is a line with no label
                //--

            }
            Console.ReadLine();
        }
    }

    public static class StringHelpers
    {
        public static string GetLabelFromLine(this string line)
        {
            string label = line.GetMatch(@"^?:(\s)"); //???????????????
            if (!label.IsNullOrEmpty())
                return label;
            else
                return "";
        }

        public static string StripLabelFromLine(this string line)
        {
            return ...//???????????????
        }

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

    public static class RegexHelpers
    {
        public static string GetMatch(this string text, string regex)
        {
            Match match = Regex.Match(text, regex);
            if (match.Success)
            {
                string theMatch = match.Groups[0].Value;
                return theMatch;
            }
            else
            {
                return null;
            }
        }
    }
}

© Stack Overflow or respective owner

Related posts about c#

Related posts about regex