How to find rich text tag matches within other tags using Regex?

Hello everybody! :slight_smile:
I have this one problem, what pattern I should use to find all rich text color tags within other color tags?

So for example I have this input:
<color=yellow>Hello <color=cyan>World!</color></color>

And remove, by replacing with empty string matched tags and have this as an input after:
<color=yellow>Hello World!</color>

It could be even more tags within, for example:
<color=yellow>Hello my <color=cyan>name</color> is <color=gray>Kite <color=white>Watson!</color></color></color>

And have this after:
<color=yellow>Hello my name is Kite Watson!</color>

The reason I need this is because I use Regex to apply code highlighter to text in text box and some keywords are colorized within comments, like in below example

5051990--495809--wefwef.PNG

So I want to check and remove if there are any color tags within color tags, like in this comment example.

I’m pretty new to Regex, so currently a bit lost and not sure what to do. Can someone give me some advice on how can I accomplish this? :slight_smile: Thank you!

Do you have a functioning regex to find one color tag and retrieve its inner text? If so, you can just run that again on the inner text.

Hey there! Thanks for the reply! :slight_smile: The issue is that I have no idea how to create a pattern that would get inner rich tags, I am kind of new to Regex. Could you give me some advice on what regex pattern I should use for this purpose? :slight_smile:

Do you need to preserve other tags, and are there multiple instances of inner/outer colors on the same line? What I mean is do you have things like this?

<color=yellow><b><color=red>Bold stuff</color></b></b>blah blah no color<color=blue><color=red>other stuff</color></color>

Also, are all your color tags closing on the same line they open, and do your lines always start with the color tag you want to keep?

Hey there!
Thanks for the reply! What I ended up with is something like this:

    Regex tags = new Regex(@"<color=#.*?>|<\/color>");
    MatchCollection matches = tags.Matches(c);

    bool hasStarted = false;
    int innerTags = 0;

    const string tempStart = "¬¬¬¬¬¬¬Â";
    const string tempEnd = "Â~Â~Â~Â~";
    foreach (Match match in matches)
    {
        if (match.Value.Contains("<color=#"))
        {
            if (hasStarted)
            {
                var cBuilder = new StringBuilder(c);
                cBuilder.Remove(match.Index, match.Length);
                cBuilder.Insert(match.Index, tempStart);

                c = cBuilder.ToString();

                innerTags++;
            }
            else
            {
                hasStarted = true;
            }
        }
        else if (match.Value.Equals("</color>"))
        {
            if (innerTags > 0)
            {
                var cBuilder = new StringBuilder(c);
                cBuilder.Remove(match.Index, match.Length);
                cBuilder.Insert(match.Index, tempEnd);

                c = cBuilder.ToString();

                innerTags--;
            }
            else if (innerTags <= 0)
            {
                hasStarted = false;
            }
        }
    }
    c = c.Replace(tempStart, string.Empty);
    c = c.Replace(tempEnd, string.Empty);

It works quite well, but still need to do more checking, the problem is that, I apply these rich text tags as a code highlighter, to my text in the textbox, but the issue is that what if I write a comment and I include a color tag that doesn’t have a closing, I need to check and if found one like that, close it so it wouldn’t cause any issues. I have some ideas on how to do this, but are there any better ways in fixing this or even applying code highlighter in general, without using rich text tags?

1 Like