[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: succinct awk code to check for paired parantheses?
From: |
Wolfgang Laun |
Subject: |
Re: succinct awk code to check for paired parantheses? |
Date: |
Thu, 25 Mar 2021 07:33:16 +0100 |
Gawk Regex experts might like to comment on my statement re escaping (not).
On Wed, 24 Mar 2021 at 22:35, Manuel Collado <mcollado2011@gmail.com> wrote:
> function paired(text) {
> while (gsub(/\([^()[\]{}]*\)/,"",text) \
> + gsub(/\[[^()[\]{}]*\]/,"",text) \
> + gsub(/\{[^()[\]{}]*\}/,"",text) \
> ) {} # just replace pairs
> return text !~ /[()[\]{}]/
> }
>
I had been thinking along similar lines. As I always try to reduce the use
of escaping, I came up with:
function isBalanced(s){
gsub( /[^][)(}{]+/, "", s ); # only ']' is tricky in [...]
while( gsub( /\(\)|{}|\[]/, "", s ) ); # an empty {} is not "magic",
and neither are unbalanced ']' and ')'
return length(s) == 0;
}
Regex documentations (across languages) hardly ever mention that my
parsimonious use of escaping is according to the book. The rule would be
something like "if it cannot be parsed as a correct construct it shall be
used as a literal character." (I think of it as the "un-printed small
print".)
Thank you
Wolfgang