我需要剝去「標籤」斷開串的前部,例如什麼Regex可以去除例如字符串左邊的「note:」和「firstName:」?
注:這是一個註解
需要返回:
注
和
這是一記
我產生了以下的代碼示例但我有與正則表達式的麻煩。
我需要在這兩個是什麼代碼????????下面的區域,以便我可以在評論中看到所需的結果?
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;
}
}
}
}
新增
@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; }
}
}
解決:
好吧,我得到它的工作,再加上加了一點黑客採取用空格標籤的關懷和它正是我想要的,謝謝!
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; }
}
}
看起來像一個錯字......你有labelLinePair.Label = match.Groups [「行」]。的ToString();.它應該是labelLinePair.Line = match.Groups [ 「線」]的ToString(); (你要指定標籤兩次,而不是分配線) – Keltex 2010-06-01 16:35:58