C# 什么正则表达式可以剥离,例如“什么”;注:";及;名字:“;从字符串的左边开始?
我需要剥掉字符串前面的“标签”,例如 注:这是一张便条 需要返回: 注 及 这是一张便条 我已经生成了下面的代码示例,但是在正则表达式方面遇到了问题 这两个版本中我需要什么代码????????下面的区域,以便我获得评论中显示的所需结果?C# 什么正则表达式可以剥离,例如“什么”;注:";及;名字:“;从字符串的左边开始?,c#,regex,string,C#,Regex,String,我需要剥掉字符串前面的“标签”,例如 注:这是一张便条 需要返回: 注 及 这是一张便条 我已经生成了下面的代码示例,但是在正则表达式方面遇到了问题 这两个版本中我需要什么代码????????下面的区域,以便我获得评论中显示的所需结果? using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Text.RegularExpressions; namesp
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;
}
}
}
}
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; }
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Text.RegularExpressions;
命名空间TestRegex8822
{
班级计划
{
静态void Main(字符串[]参数)
{
列表行=新列表();
行。添加(“注:这是一个注”);
添加(“测试:只是一个测试”);
添加(“测试:\t\t\t只是一个测试”);
Add(“firstName:Jim”);/“firstName”是一个标签,因为它不包含空格
line.Add(“她对他说:跟我来。”);//这不是一个标签,因为冒号前面有一个空格
行。添加(“说明:这是第一个说明”);
lines.Add(“描述:这是第二个描述”);//冒号后没有空格
行。添加(“这是没有标签的行”);
foreach(行中的var行)
{
Console.WriteLine(StringHelpers.GetLabelFromLine(line));
Console.WriteLine(StringHelpers.StripLabelFromLine(行));
Console.WriteLine(“--”);
//注
//这是一张便条
//--
//试验
//只是个测试
//--
//试验
//只是个测试
//--
//名字
//吉姆
//--
//
//她对他说:跟我来。
//--
//描述
//这是第一个描述
//--
//描述
//这是第一个描述
//--
//
//这是一条没有标签的线
//--
}
Console.ReadLine();
}
}
公共静态类StringHelper
{
公共静态字符串GetLabelFromLine(此字符串行)
{
string label=line.GetMatch(@“^?:(\s)”);/???????????????
如果(!label.IsNullOrEmpty())
退货标签;
其他的
返回“”;
}
公共静态字符串StripLabelFromLine(此字符串行)
{
返回…/???????????????
}
公共静态bool IsNullOrEmpty(此字符串行)
{
返回字符串.IsNullOrEmpty(行);
}
}
公共静态类RegexHelpers
{
公共静态字符串GetMatch(此字符串文本,字符串正则表达式)
{
Match=Regex.Match(文本,Regex);
如果(匹配成功)
{
字符串match=match.Groups[0]。值;
返回匹配项;
}
其他的
{
返回null;
}
}
}
}
补充
@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("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;
}
}
}
}
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; }
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Text.RegularExpressions;
命名空间TestRegex8822
{
班级计划
{
静态void Main(字符串[]参数)
{
列表行=新列表();
行。添加(“注:这是一个注”);
添加(“测试:只是一个测试”);
添加(“测试:\t\t\t只是一个测试”);
Add(“firstName:Jim”);/“firstName”是一个标签,因为它不包含空格
lines.Add(“first name:Jim”);/“first name”不是标签,因为它包含空格
行。添加(“说明:这是第一个说明”);
lines.Add(“描述:这是第二个描述”);//冒号后没有空格
行。添加(“这是没有标签的行”);
foreach(行中的var行)
{
LabelLinePair llp=line.GetLabelLinePair();
控制台写入线(llp标签);
控制台写入线(llp线);
Console.WriteLine(“--”);
}
Console.ReadLine();
}
}
公共静态类StringHelper
{
公共静态LabelLinePair GetLabelLinePair(此字符串行)
{
正则表达式正则表达式=新正则表达式(@“(?。+):\s*(?。+)”);
匹配=正则表达式匹配(行);
LabelLinePair LabelLinePair=新LabelLinePair();
labelLinePair.Label=match.Groups[“Label”].ToString();
labelLinePair.Line=match.Groups[“Line”].ToString();
返回标签空气;
}
}
公共类LabelLinePair
{
公共字符串标签{get;set;}
公共字符串行{get;set;}
}
}
解决了的:
好的,我成功了,另外还添加了一个小技巧来处理带有空格的标签,这正是我想要的,谢谢强>
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; }
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Text.RegularExpressions;
命名空间TestRegex8822
{
班级计划
{
静态void Main(字符串[]参数)
{
列表行=新列表();
行。添加(“注:这是一个注”);
添加(“测试:只是一个测试”);
添加(“测试:\t\t\t只是一个测试”);
Add(“firstName:Jim”);/“firstName”是一个标签,因为它不包含空格
lines.Add(“first name:Jim”);/“first name”不是标签,因为它包含一个s
(?: *([^:\s]+) *: *)?(.+)