2016-03-06 30 views
1

我想在c#中使用正則表達式去除部分文本。文字看起來像:使用c#中的正則表達式去除一些文本#

BEGIN:VNOTE 
VERSION:1.1 
BODY;CHARSET=UTF-8;ENCODING=QUOTED-PRINTABLE:Penguins are among the most popular of all birds. They only live in and around the South Pole and the continent of Antarctica.No wild penguins live at the North Pole. There are many different kinds of penguins. The largest penguin is called the Emperor Penguin, and the smallest kind of penguin is the Little Blue Penguin. There are 17 different kinds of penguins in all, and none of them can fly 

當我想從文本部分

BEGIN:VNOTE 
VERSION:1.1 
BODY;CHARSET=UTF-8;ENCODING=QUOTED-PRINTABLE: 

BEGINPRINTABLE:之間的文字刪除可以是不同的結果。 所以我寫的代碼(最新版本):

var start = "BEGIN"; 
var end = "PRINTABLE:"; 
var regEx = string.Format("{0}(.*|\n){1}", start, end); 
var result = Regex.Replace(sourceText, regEx, string.Empty); 

但它不工作。我嘗試了許多不同的正則表達式,結果相同。任何想法如何我的正則表達式應該看起來?

謝謝你的任何建議。

+0

也許您需要將您的邏輯逆轉並匹配您想要檢索的內容,而不是刪除不需要的內容。這是一個可能的方法嗎? – Filkolev

+0

@Filkolev:這裏的情況要求匹配**不需要的**部分,並用'empty'字符串替換它。這會更簡單。 – 2016-03-06 11:44:57

+0

爲作業使用正確的工具。編碼不會被引用 - 可打印,您不應該忽略字符集。你最好使用一個合適的VCard/VCalendar/VNote解析器庫,它可以正確讀取這種格式。 – CodeCaster

回答

2

您應該在BEGINPRINTABLE之間匹配everything。遵循正則表達式做同樣的事情。

正則表達式:使用BEGIN.+?PRINTABLE:

標誌:

  • g全局搜索。

  • s允許.比賽newline

更換做:與空字符串替換。

Regex101 Demo

編輯#1:更改正則表達式變得更加lazy。感謝Jan進行編輯。

+2

單線模式下的點星湯會消耗掉所有的東西,直到字符串的末尾,然後回溯。我改變了[你的解決方案](https://regex101.com/r/nM1vE4/2)以支持一個懶惰的量詞,你是否看到*顯着減少步驟(428對71)?有時更重要的是結束(並最終失敗),而不是嘗試每一個可能的步驟。 – Jan

+1

@Jan:謝謝,我現在意識到了。這是對我的解決方案的重大改進。 – 2016-03-06 11:52:01

+1

要說清楚:[你的解決方案](https://regex101.com/r/oE5pH0/1) - 60590步驟和[懶惰量詞](https://regex101.com/r/oE5pH0/2) - 還有71個步驟。 – Jan

0

我一直在解析文本文件40年,並使用正則表達式很多。正則表達式不會總是工作,並不是解析每種類型的文本文件的最佳工具。下面使用的技術可以工作,並且更適合將文件解析爲多個部分

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.IO; 

namespace ConsoleApplication1 
{ 
    class Program 
    { 
     public enum State 
     { 
      FIND_BEGIN, 
      FIND_END 
     } 
     static void Main(string[] args) 
     { 

      string input = 
       "BEGIN:VNOTE\n" + 
       "VERSION:1.1\n" + 
       "BODY;CHARSET=UTF-8;ENCODING=QUOTED-PRINTABLE:Penguins are among the most popular of all birds. They only live in and around the South Pole and the continent of Antarctica.No wild penguins live at the North Pole. There are many different kinds of penguins. The largest penguin is called the Emperor Penguin, and the smallest kind of penguin is the Little Blue Penguin. There are 17 different kinds of penguins in all, and none of them can fly\n"; 
      StringReader reader = new StringReader(input); 

      StringBuilder builder = new StringBuilder(); 
      StringWriter writer = new StringWriter(builder); 

      State state = State.FIND_BEGIN; 
      string inputLine = ""; 
      Boolean end = false; 
      while ((inputLine = reader.ReadLine()) != null) 
      { 
       switch (state) 
       { 
        case State.FIND_BEGIN : 
         if(inputLine.StartsWith("BEGIN:")) 
         { 
          writer.WriteLine(inputLine); 
          state = State.FIND_END; 
         } 
         break; 
        case State.FIND_END : 
         if (inputLine.StartsWith("BODY;")) 
         { 
          writer.WriteLine(inputLine.Substring(0, inputLine.IndexOf(":") + 1)); 
          end = true; 
         } 
         else 
         { 
          writer.WriteLine(inputLine); 
         } 
         break; 
       } 
       if (end) break; 
      } 
      string output = builder.ToString(); 

     } 
    } 
}