2016-09-05 3 views
2

Comment puis-je accomplir la tâche d'extraction de texte dans les balises et de les transformer?Extraire et transformer

Exemple:

out formatted

entrée:

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

Out: Ceci est en italique Texte gras

J'utilise ce code pour extraire le texte soit interpoler les balises, mais le problème est qu'il ne prend le texte de la première balise

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); 

} 

Ce que je voudrais accomplir et exactement ce qui se passe dans l'éditeur stackoverflow ...

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

     richTextBox1.AppendText("Bold Text"); 

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

     richTextBox1.AppendText("Normal Text"); 

en gras texte **** utilisation et italic **

+0

Vous pouvez lire l'article SO http://stackoverflow.com/questions/7377344/how-to-write-a-parser-in-c "Comment écrire un analyseur". Je pense qu'un peu de recherche dans l'analyse de texte et les analyseurs de syntaxe vous aideront. – PhillipH

+0

La sortie souhaitée est le texte du plan ou le texte ** formaté **? Parce que si c'est le dernier, c'est quelque chose de spécifique à * où * vous placerez ce texte après. – Andrew

+0

Win formrs merci –

Répondre

0

est ici une façon de faire ce que je pense que vous avez besoin:

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

Et ceci est le résultat si vous utilisez un RichTextBox standard:

End result in RichTextBox control

Bien sûr, cela est juste un point de départ. Si vous voulez combiner des formats ou toute autre fonctionnalité, vous devrez l'ajouter. ;)

+0

Vraiment merci, vous aidez beaucoup ... merci –

0

Cela devrait faire le travail pour vous:

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

Sortie:

Ceci est en italique gras Texte

Note: Cela n'extraire le texte entre les balises.

+0

Cool, mais comment puis-je ajouter au formatage? Ex: if (tags = txtBold) {bold (s1)} –

+0

@KawyllainyVi Les objets 'chaîne' n'ont pas de formatage, UI Framworks comme WinForms, WPF et ASP.NET. Vous devez avoir besoin de montrer comment vous affichez le texte à l'interface utilisateur pour que nous vous le disions. –

+0

J'espère que cet exemple vous aidera à visualiser ce que j'ai l'intention de faire http://pastebin.com/XcJZ2RAw –