2

How can I accomplish the task of extracting text within tags and transform them ?

Example:

out formatted

input:

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

Out: This is italic Bold Text

I'm using this code to extract the text between the tags , but the problem is that it only takes the text of the first 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);

}

What I would like to accomplish and exactly what happens in stackoverflow Editor ...

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

        richTextBox1.AppendText("Bold Text");

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

        richTextBox1.AppendText("Normal Text");

To bold text use **** and italic **

Andrew
  • 7,602
  • 2
  • 34
  • 42
  • You might read the SO post http://stackoverflow.com/questions/7377344/how-to-write-a-parser-in-c "How to Write a Parser". I think a bit of research into text parsing and syntax analzers will help you. – PhillipH Sep 05 '16 at 16:48
  • The desired output is the plan text or **formatted** text? Because if it's the latter, that's something specific to *where* you will place that text afterwards. – Andrew Sep 05 '16 at 17:09

2 Answers2

0

This should do the work for you:

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

Output:

This is italic Bold Text

Note: This will only extract the text between the tags.

Raktim Biswas
  • 4,011
  • 5
  • 27
  • 32
  • Cool, but how can I add to formatting ? Ex: if (tags = txtBold) { bold(s1) } – Kawyllainy Vi Sep 05 '16 at 17:23
  • @KawyllainyVi `string` objects don't have formatting, UI Framworks like WinForms, WPF, and ASP.NET do. You need to need to show how you display the text to the UI for us to tell you that. – Scott Chamberlain Sep 05 '16 at 17:33
  • I hope this example will help you visualize what I'm intending to do http://pastebin.com/XcJZ2RAw – Kawyllainy Vi Sep 05 '16 at 17:57
  • @KawyllainyVi check [this](http://stackoverflow.com/questions/26842232/richtextbox-font-style-button-for-bold-and-or-italic-how-to-code-so-more-than-o) – Raktim Biswas Sep 05 '16 at 18:40
0

Here's a way of doing what I think you need:

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

And this is the result if you use a standard RichTextBox:

End result in RichTextBox control

Of course this is just a starting point. If you want to combine formats or any other feature, you will have to add that. ;)

Andrew
  • 7,602
  • 2
  • 34
  • 42