On Tue, 16-Oct-2001 at 15:27:52 +0100, Rory Winston wrote:
> Is it possible to use Perl to match any number of parentheses using a regex?
Just in case, I'd better make sure that I'm currectly understanding what
you want. I think you wanted to match this:
variable = (parenthesised expression)
and pull out the variable. The trick here is that the parenthesised
expression might contain other parenthesised expressions, so you can't just
look for the first closing paren after the opening paren.
If memory serves, this is one of the examples given as something that
regular expressions are not well suited for (counting the number of opening
parens, and matching that number of closing parens). You need more
sophisticated grammars to describe it correctly.
That said, here's something that might produce something resembling the
desired behaviour:
while (s/(.*)\(.*?\)/$1/s) {
;
}
This removes the innermost parenthesised expression first, and continues
doing this until there are no parentesised expressions left. This leaves
you with something like this (after destroying the original string, of
course):
A =
B =
C =
--
Take care,
Scott \\'unsch
... You want a stupid answer? Ask me anything!
Maintained by the ILUG website team. The aim of Linux.ie is to
support and help commercial and private users of Linux in Ireland. You can
display ILUG news in your own webpages, read backend
information to find out how. Networking services kindly provided by HEAnet, server kindly donated by
Dell. Linux is a trademark of Linus Torvalds,
used with permission. No penguins were harmed in the production or maintenance
of this highly praised website. Looking for the
Indian Linux Users' Group? Try here. If you've read all this and aren't a lawyer: you should be!