2010-06-01 38 views
2

我需要剝去「標籤」斷開串的前部,例如什麼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; } 
    } 

} 
+0

看起來像一個錯字......你有labelLinePair.Label = match.Groups [「行」]。的ToString();.它應該是labelLinePair.Line = match.Groups [ 「線」]的ToString(); (你要指定標籤兩次,而不是分配線) – Keltex 2010-06-01 16:35:58

回答

3

它可能是這樣的:

Regex myreg = new Regex(@"(?<label>.+):\s*(?<text>.+)"); 

Match mymatch = myreg.Match(text); 

if(mymatch.IsMatch) 
{ 
    Console.WriteLine("label: "+mymatch.Groups["label"]); 
    Console.WriteLine("text: "+mymatch.Groups["text"]); 
} 

我用上面的命名相匹配,但你可以沒有他們。另外,我認爲這比做兩個方法調用更有效率。一個正則表達式獲得文本和標籤。

+0

我在上面加入了這個想法,但是它不匹配那個正則表達式上的任何文本,我需要在正則表達式中改變什麼? – 2010-06-01 16:31:19

+0

\應該被轉義,即「(?

+0

@Edward ...請參閱以上我的評論,您有一個小錯字 – Keltex 2010-06-01 16:37:03

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); 
    } 
} 
+0

是的,我可以用分裂代替正則表達式,但我希望能夠在以後改進此代碼,這樣我可以拉斷特定標籤與前綴,例如「>注意」,「?注意「和」*注「可能意味着三種不同的東西,因此我想要得到一個正則表達式代碼庫,所以我可以調整正則表達式,而不是創建更多和更復雜的代碼與拆分,等等。 – 2010-06-01 16:13:55

+0

很容易做到這一點。只需要你的方法採用正則表達式或字符串來檢查字符串中找到的標籤。 – jball 2010-06-01 16:17:46

+3

正則表達式對我來說也有點過分。搜索第一個冒號後,如果你發現了標籤的模式匹配 – n8wrl 2010-06-01 16:45:31

1

此正則表達式的作品(see it in action on rubular):

(?: *([^:\s]+) *: *)?(.+) 

這抓住了標籤,如果有的話,到\1,與身體成\2

它有足夠津貼空格,所以標籤可以縮進等