D
D
dtestyk2012-04-01 01:55:48
Programming
dtestyk, 2012-04-01 01:55:48

Recursive(?) regular expressions in tcl?

I have the following tcl program:
#!/usr/bin/tclsh
set p {}
while {[gets stdin c] >= 0} {
set r $p$c
#puts [execute r]
set p {}
regexp {^( [^\}]*\{)(?:[^{}]*(?:\{[^{}]*\})?)?$} $r -> p
puts -nonewline $p
flush stdout
}
It can be seen that you can open several brackets at once in one command (here "([^\}]*\{)"). Is it possible to do the same for closing ones, so that each closing one closes exactly one opening one, without changing the code much (ideally, only the regular expression)?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
MikhailEdoshin, 2012-04-01
@MikhailEdoshin

I didn't quite understand the question, maybe, but if you need to match brackets with a regular expression, then it's basically impossible. Regular expressions can only describe regular grammars, this is the third type of grammar, they can be parsed automatically without memory, and bracket matching is a grammar of the second type, more powerful, they need a parser. You can match parentheses if you have, say, no more than five pairs per line (that is, a limited number), but generally not.
Although modern regular expressions are quite powerful with a bunch of all sorts of additional features, I don’t think they will add anything like that there - there are significantly different algorithms.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question