Well, it is possible, but notoriously difficult to check for balancing parens or brackets with regexes.

I would suggest that you simply count the opening and the closing brackets and check that the numbers match. This approach, however, would not detect an error in something like this:

Code

[]][

If you want to detect this as an error, then set a counter to 0, read your data, increment the counter for any opening bracket, decrement it for every closing bracket, and report an error if the counter ever becomes negative or if it is not zero at the end.

print "Balanced\n" if isBalanced($str); print "Not Balanced\n" if not isBalanced($str);

What if we also monitored for a closing bracket as well, and if there are more closing brackets then opening brackets at any moment an error is thrown. This check in addition to the check at the end for '0' would do the trick....Agree,disagree. I think we would now be able to catch unbalanced brackets? Thanks again for your help. I kind of knew the regex approach would be difficult. I doubt I would even like to try.

###############

Thanks FishMonger for pointing out of the Regex module. I'll check it out. So many modules, so so many.

Thank you again, zap

Lastly, is my previous approach any good. It doesn't seem Perlish in my opinion. Can somebody help modify my code (or even my new code ) so that it hints of a 'Perl' veteran.

Being well-formed, i.e., properly nested, is implicit in all of the OP's examples. That qualification also merely paraphrases your earlier statement about the "error" (read "not well-formed") in "[]]["--unless, of course, "[]][" "...may very well be well-formed in some contexts, and not in other." However, if "[]][" is well-formed "in some contexts," then its instance not categorically an error.

Your excellent examples clearly exemplify an operational definition of "well-formed," in this case. The notions of "nesting" and "proper closure" can be included as necessary conditions in a stipulative definition of "well-formed."

I don't think defining "well-formed" is difficult here. The difficulty comes in creating an effective parsing engine that tests for this "well-formedness."

Thank you. I strive to give quality examples. Ambiguity is a nemisis. You brought up the notion of well-formed.

For some reason you decided to point out: 'Both of your routines say that '[[ ][[ ]]]' is balanced. The r/l brackets are, but it's not well-formed. '

From the onset, my quest was to make sure the brackets were closed 'properly'. I explicitly quoted properly because many may debate what properly means. I gave examples of my quest in hopes of clarifying any misunderstanding.

You also stated:

The notions of "nesting" and "proper closure" can be included as necessary conditions in a stipulative definition of "well-formed."

This is true. However, just because something can be included does not mean it should be included.

Lastly, I gave examples of what my quest was and for some reason you brought up how my results where not well-formed. I sought for the brackets to be closed 'properly' and 'properly' may not be equivalent to 'well-formed'.

In any case, thank you for your comments. Thank you to everyone. Until the next time zap