-1

I am working on a project where I have strings with lots of brackets like ((A and B and (C or (D and E))) or F). I need to find a way to find the pairs and style or color them to show they are matching. For example, the brackets around "D and E" would be green, and the ones around "C or (D and E)" would be blue. For matching pairs I was using a stack but couldn't find a way to set colors, if theres a better way I'm open to try it. I'm working in asp.net and C#. Thanks!

Edit: here is the code that generates the string. terms is a list of values, innerConnector is either "and" or "or" based on user input, and child groups are groups within the larger group. (A and (B or C)) A would be a term, (B or C) would be a child group. I want to take the string this returns and apply the colors to the parentheses.

public string toString()
    {
        string str = "(";
        if(terms.Count > 0)
        {
            for(int i = 0; i < terms.Count; i++)
            {
                if(i == 1)
                {
                        str += terms[i];
                }
                else
                {
                    str += " " + innerConnector + " " + terms[i];
                }
            }
        }
        if(children != null)
        {
            for(int i = 0; i < terms.Count; i++)
            {
                if(i == 1 && terms.Count == 0)
                {
                    str += childGroups[i].toString();
                }
                else
                {
                    str += " " + innerConnector + " " childGroups[i].toString();    
                } 
            }
        }
        str += ")";
    }
  • You shouldn't use colors to code as that's not how it's done in the real world. If you code a lot, you'll get used to the bracket alignment. Although you might be talking about parenthesis? You can space those out and it shouldn't affect the code. You can put them on separate lines but I would not tamper your IDE for colors. – Ben Adamsky Jul 21 '15 at 15:52
  • Why is javascript included in this? Also, supposing brackets can't do this `(A and [B and C) and D]` you could just cycle through each open bracket, assign a colour, then cycle through the close brackets and assign the same colours in reverse order. – JBux Jul 21 '15 at 15:53
  • I should clarify, I'm not using the colors to code. I'm using them for the presentation of information so its easier to follow. I have the string being made given user input, I want to present the string on the webpage with the pairs of parentheses colored or in some other way showing the pairs so the user can see the groupings easier. – Ryan Weissman Jul 21 '15 at 15:55
  • I would suggest looking into some kind of 'pretty print' library. They will at least make it look cleaner. http://stackoverflow.com/questions/4810841/how-can-i-pretty-print-json-using-javascript – mwilson Jul 21 '15 at 16:01
  • 1
    Without providing any code at all you'll probably not get an answer and/or get downvoted. Anyway, assuming that the string is perfect and all opening brackets have appropriate closing pairs, it shouldn't actually be too difficult. In theory you read each character in turn, when you encounter an opening bracket give it a colour and put this in a [Stack](https://msdn.microsoft.com/en-us/library/3278tedw%28v=vs.110%29.aspx) which is FILO, then every time you encounter a closing bracket pop the last colour off the stack. Done! – Equalsk Jul 21 '15 at 16:08
  • Are you trying to find matching brackets (parentheses, actually) as suggested in your title, or are you trying to figure out how to make sections a different color? – Jonathan Wood Jul 21 '15 at 16:16
  • Both. More color than find. I can use a stack and get the indexes of the open and closing pairs but from there I don't know how to change the color. If there is a better way to find them that would make the pairing/coloring easier I would switch to that. – Ryan Weissman Jul 21 '15 at 16:18
  • Forgive me if I'm being stupid (long day), but change the colour where? At the moment it's just a string which has no concept of colour. I assume you mean once it's displayed somewhere on your webpage? Surely once you know the indices of each bracket it's easy enough to transform this into some HTML markup and display it? – Equalsk Jul 21 '15 at 16:25

1 Answers1

1

I see you've removed javascript from your question tag. Disregard below then.


This was quite fun to have a play with actually. It's not the most elegant solution!

I've slightly commented the code because I'm such a lovely chap.

// List of colours we'll use.
var colors = ['red', 'blue', 'purple', 'orange', 'green'];

// Define the div and it's contents
var div = document.getElementById('colors')
var string = div.innerHTML;

// A function to return a span object with coloured text.
color_span = function(color, inner) {
    return '<span style="color:' + color + '">' + inner + '</span>'
}

var color_count = 0;
var return_html = '';

// Cycle through each character in the string
for (i=0; i < string.length; i++) {
    // If it's an open bracket, add a coloured span to the return_html and increment the colour counter
    if ( string[i] == '(' ) {
        return_html += color_span(colors[color_count], string[i]);
        color_count++;
    // If close bracket add coloured span and decrement the colour counter.
    } else if ( string[i] == ')' ) {
        color_count--;
        return_html += color_span(colors[color_count], string[i]);
    // If neither just add the character.
    } else {
        return_html += string[i];
    }
}
// Change the div's html.
div.innerHTML = return_html

JSFiddle

JBux
  • 1,394
  • 8
  • 17