正则表达式可以从字符串的左边删除例如"note:"和"firstName:"?

Edw*_*uay 2 c# regex string

我需要从字符串的前面剥去"标签",例如

注意:这是一张纸条

需要返回:

注意

这是一张纸条

我已经生成了以下代码示例,但我遇到了正则表达式的问题.

我需要什么代码????????? 以下区域,以便我在评论中显示所需的结果?

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;
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

添加

@Keltex,我将你的想法结合如下,但它不匹配任何文本(所有条目都是空白的),我需要在正则表达式中调整什么?

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("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");

            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 labelLinePair = new LabelLinePair();
            labelLinePair.Label = match.Groups["label"].ToString();
            labelLinePair.Line = match.Groups["line"].ToString();
            return labelLinePair;
        }
    }

    public class LabelLinePair
    {
        public string Label { get; set; }
        public string Line { get; set; }
    }

}
Run Code Online (Sandbox Code Playgroud)

解决了:

好吧,我得到它的工作,加上一点点黑客来处理带有空格的标签,这正是我想要的,谢谢!

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("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; }
    }

}
Run Code Online (Sandbox Code Playgroud)

jba*_*all 5

难道你不能简单地在第一个冒号上拆分字符串,或者如果没有冒号就没有标签?

public static class StringHelpers 
{ 
    public static string GetLabelFromLine(this string line) 
    { 
         int separatorIndex = line.IndexOf(':');
         if (separatorIndex > 0)
         {
            string possibleLabel = line.Substring(0, separatorIndex).Trim();
            if(possibleLabel.IndexOf(' ') < 0) 
            {
                return possibleLabel;
            }
         }
         else
         {
            return string.Empty;
         }        
     } 

    public static string StripLabelFromLine(this string line) 
    { 
        int separatorIndex = line.IndexOf(':');
         if (separatorIndex > 0)
         {
            return line.Substring(separatorIndex + 1, 
                   line.Length - separatorIndex - 1).Trim();
         }
         else
         {
            return line;
         }      
    } 

    public static bool IsNullOrEmpty(this string line) 
    { 
        return String.IsNullOrEmpty(line); 
    } 
} 
Run Code Online (Sandbox Code Playgroud)

  • 正则表达式对我来说也有点矫枉过正.搜索第一个冒号,如果您确实需要,请稍后在标签上进行模式匹配. (3认同)