2016-09-05 2 views
2

Wie kann ich die Aufgabe ausführen, Text innerhalb von Tags zu extrahieren und zu transformieren?Extrahieren und Transformieren

Beispiel:

out formatted

Eingang:

[txtItalic]This is italic[/txtItalic] [txtBold] Bold Text [/txtBold] 

Out: Dies ist kursiv Bold Text

ich diesen Code bin mit der extrahieren Text sein tweenen die Tags, aber das Problem ist, dass es nur den Text des ersten Tag

string ExtractString(string s, string tag) 
{ 
    var startTag = "[" + tag + "]"; 
    int startIndex = s.IndexOf(startTag) + startTag.Length; 
    int endIndex = s.IndexOf("[/" + tag + "]", startIndex); 
    return s.Substring(startIndex, endIndex - startIndex); 

} 

Was möchte ich erreichen und genau das, was in Stackoverflow-Editor geschieht ...

  richTextBox1.SelectionFont = new Font(richTextBox1.Font, FontStyle.Bold); 

     richTextBox1.AppendText("Bold Text"); 

     richTextBox1.SelectionFont = new Font(richTextBox1.Font, FontStyle.Regular); 

     richTextBox1.AppendText("Normal Text"); 

zu fett nimmt Verwendung Text **** und kursiv **

+0

Sie könnten die SO-Post lesen http://stackoverflow.com/questions/7377344/how-to-write-a-parser-in-c "Wie schreibe ich einen Parser". Ich denke, ein bisschen Forschung in Textanalyse und Syntax-Analyse wird Ihnen helfen. – PhillipH

+0

Die gewünschte Ausgabe ist der Plantext oder ** formatierter ** Text? Denn wenn es Letzteres ist, ist das etwas Spezifisches für * wo * du diesen Text hinterher plazierst. – Andrew

+0

Win formers danke –

Antwort

0

Hier ist eine Art und Weise zu tun, was ich glaube, Sie brauchen:

private void YourMethod() 
{ 
    Process("[txtItalic]This is italic[/txtItalic], this is normal, [txtBold]Bold Text[/txtBold] and now [txtUnderline]Underlined Text[/txtUnderline]. The end."); 
} 

private void Process(string textWithTags) 
{ 
    MatchCollection matches = Regex.Matches(textWithTags, @"\[(\w*)\](.*)\[\/\1\]"); 

    int unformattedStartPosition = 0; 
    int unformattedEndPosition = 0; 
    foreach (Match item in matches) 
    { 
     unformattedEndPosition = textWithTags.IndexOf(item.Value); 

     // Add unformatted text. 
     AddText(textWithTags.Substring(unformattedStartPosition, unformattedEndPosition - unformattedStartPosition), FontStyle.Regular); 

     // Add formatted text. 
     FontStyle style = GetStyle(item.Groups[1]); 
     AddText(item.Groups[2].Value, style); 

     unformattedStartPosition = unformattedEndPosition + item.Value.Length; 
    } 

    AddText(textWithTags.Substring(unformattedStartPosition), FontStyle.Regular); 
} 

private FontStyle GetStyle(Group group) 
{ 
    switch (group.Value) 
    { 
     case "txtItalic": 
      return FontStyle.Italic; 
     case "txtBold": 
      return FontStyle.Bold; 
     case "txtUnderline": 
      return FontStyle.Underline; 
     default: 
      return FontStyle.Regular; 
    } 
} 

private void AddText(string text, FontStyle style) 
{ 
    if (text.Length == 0) 
     return; 

    richTextBox.SelectionFont = new Font(richTextBox.SelectionFont, style); 
    richTextBox.AppendText(text); 
} 

Und das ist das Ergebnis, wenn Sie einen Standard RichTextBox verwenden:

End result in RichTextBox control

Natürlich ist dies nur ein Startpunkt. Wenn Sie Formate oder andere Funktionen kombinieren möchten, müssen Sie das hinzufügen. ;)

+0

Wirklich danke, Sie helfen viel ... danke –

0

Dies sollte die Arbeit für Sie tun:

string s = "[txtItalic]This is italic[/txtItalic] [txtBold] Bold Text [/txtBold]"; 
//creating a list of tags 
List<string> tags = new List<string>(); 
tags.Add("txtItalic"); 
tags.Add("txtBold"); 
//iterating through each of the tags 
foreach (string tag in tags) 
{ 
    var startTag = "[" + tag + "]"; 
    int startIndex = s.IndexOf(startTag) + startTag.Length; 
    int endIndex = s.IndexOf("[/" + tag + "]", startIndex); 
    string s1 = s.Substring(startIndex, endIndex - startIndex); 
    Console.Write(s1); 
} 

Ausgang:

Dies ist kursiv Bold Text

Hinweis: Dadurch wird nur der Text zwischen den Tags extrahieren.

+0

Cool, aber wie kann ich die Formatierung hinzufügen? Bsp .: if (tags = txtBold) {bold (s1)} –

+0

@KawyllainyVi 'string' Objekte haben keine Formatierung, UI-Framworks wie WinForms, WPF und ASP.NET tun dies. Sie müssen zeigen, wie Sie den Text auf der Benutzeroberfläche anzeigen, damit wir Ihnen das mitteilen können. –

+0

Ich hoffe, dieses Beispiel wird Ihnen helfen, zu visualisieren, was ich vorhabe zu tun http://pastebin.com/XcJZ2RAw –