2012-03-30 18 views
0

の周りにテキストをキャプチャする:私はリテラルStatus:に基づいてテキストを解析し、アイテム、タイトル、説明行とステータスを持つ各の配列を取得したい正規表現は、私のようなテキスト持つリテラル

Title A 
some description on a few lines, there may be empty lines here 
some description on a few lines 
Status: some random text 
Title B 
some description on a few lines, there may be empty lines here 
some description on a few lines 
Status: some other random text 
Title C 
some description on a few lines, there may be empty lines here 
some description on a few lines 
Status: some other random text 

を。私はC#4.0を使用しています。

+5

[あなたは何をしようとしている?](http://mattgemmell.com/2008/12/08/what-have-you-tried/) –

+1

は、テキストです'string'や' string [] 'やその他の構造体として与えられていますか?結果がどのように見えるかの例を挙げることができますか? –

+0

「真実」を伝えるために、私の仕事は私の正規表現のスキルを超えているので何も試していません。 – Lincoln

答えて

1

これは(それがテキストファイルから読み込まれると仮定して)私はそれを行うだろうかです:

Regex regStatus = new Regex(@"^Status:"); 
Regex regTitle = new Regex(@"^Title:"); 
string line; 
string[] decriptionLine; 
string[] statusLine; 
string[] titleLine; 
using(TextReader reader = File.OpenText("file.txt")) 
{ 
    while(reader.Peek() > 0) 
    { 
     line = reader.ReadLine(); 
     if(regStatus.IsMatch(line)) 
     { 
      // status line, convert to array, can drop first element as it is "status" 
      statusLine = line.Split(' '); 
      // do stuff with array 
     } 
     else if(regTitle.IsMatch(line)) 
     { 
      // title line, convert to array can drop first element as it is "title" 
      titleLine = line.Split(' '); 
      // do stuff with array 
     } 
     else 
     { 
      // description line, so just split into array 
      decriptionLine = line.Split(' '); 
      // do stuff with array 
     } 
    } 
} 

あなたが望んでいた場合は、その後、配列を取り、いくつかのクラスでそれを格納することができます。私はあなたに理解させるためにそれを残します。単純な正規表現を使用して、行が "Status:"または "Title:"で始まるかどうかを確認します。真実は言われても、それは必要でさえありません。

if(line.StartsWith("Status:")) {} 
if(line.StartsWith("Title:")) {} 

各行が「ステータス」または「タイトル」で始まるかどうかをチェックします。

1

あなたが記述のようにコンテンツが構成されている場合、あなたは

string myRegEx = "^String:.*$"; 

// loop through each line in text 

    if (System.Text.RegularExpressions.Regex.IsMatch(line, myRegEx)) 
    { 
     // save the buffer into array 
     // clear the buffer 
    } 
    else 
    { 
     // save the text into the buffer 
    } 
1

は、アイテムタイプ

public class Item 
{ 
    public string Title { get; set; } 
    public string Status { get; set; } 
    public string Description { get; set; } 
} 

が続いライン

string[] lines = text.Split(new[] { "\r\n" }, StringSplitOptions.None); 

かにテキストを分割宣言したテキストをバッファリングすることができますファイルから行を読み取る

string[] lines = File.ReadAllLines(path); 

結果が今、私たちはこのソリューションは何のエラー処理を持っていないことを解析する

Item item; 
for (int i = 0; i < lines.Length; i++) { 
    string line = lines[i]; 
    if (line.StartsWith("Title ")) { 
     item = new Item(); 
     result.Add(item); 
     item.Title = line.Substring(6); 
    } else if (line.StartsWith("Status: ")) { 
     item.Status = line.Substring(8); 
    } else { // Description 
     if (item.Description != null) { 
      item.Description += "\r\n"; 
     } 
     item.Description += line; 
    } 
} 

注を行うことができます

var result = new List<Item>(); 

を保存される項目のリストを作成します。このコードは、入力テキストが常に整形式であることを前提としています。

+0

私は 'lines'に' lines [i] 'を代入してコードをいくらか簡略化し、記述部分のエラーを修正しました。 –

0
string data = @"Title A 


Status: Nothing But Net! 
Title B 
some description on a few lines, there may be empty lines here 
some description on a few lines 
Status: some other random text 
Title C 
Can't stop the invisible Man 
Credo Quia Absurdium Est 
Status: C Status"; 

string pattern = @" 
^(?:Title\s+) 
(?<Title>[^\s]+) 
(?:[\r\n\s]+) 
(?<Description>.*?) 
    (?:^Status:\s*) 
    (?<Status>[^\r\n]+) 
"; 

// Ignorepattern whitespace just allows us to comment the pattern over multiple lines. 
Regex.Matches(data, pattern, RegexOptions.Singleline | RegexOptions.Multiline | RegexOptions.IgnorePatternWhitespace) 
    .OfType<Match>() 
    .Select (mt => new 
     { 
      Title = mt.Groups["Title"].Value, 
      Description = mt.Groups["Description"].Value.Trim(), 
      Status = mt.Groups["Status"].Value.Trim() 
     }) 
     .ToList() // This is here just to do the display of the output 
     .ForEach(item => Console.WriteLine ("Title {0}: ({1}) and this description:{3}{2}{3}", item.Title, item.Status, item.Description, Environment.NewLine)); 

出力:

Title A: (Nothing But Net!) and this description: 


Title B: (some other random text) and this description: 
some description on a few lines, there may be empty lines here 
some description on a few lines 

Title C: (C Status) and this description: 
Can't stop the invisible Man 
Credo Quia Absurdium Est 
関連する問題