Re: [ILUG] Perl regex question

From: Fergal Daly (fergal at domain esatclear.ie)
Date: Tue 16 Oct 2001 - 15:53:42 IST


if you just want to grab the first "word" that comes after a "(" then

at domain words = /\([\(\)\s]*([^\s\(\)]*)/gs

should do the trick. It looks for "(" followed by as much whitespace and
"("s and ")"s as it can find and throws it all away. After that it started
capturing as many characters as possible as long as they're not whitespace,
"(" or ")". The it starts all over again. Outputting an array of all the
words it found.

If you want to be fancier and actually parse the whole thing and match the
opening and closing brackets with one-another then you have to start
embedding bits of perl in the regex but at that stage you may as well just
use one of the Parser:: modules,

Fergal

On Tue, Oct 16, 2001 at 03:27:52PM +0100, Rory Winston wrote:
> Hi,
>
> Is it possible to use Perl to match any number of parentheses using a regex?
> For example, if I have an entry like the following:
>
> A = ( abc ( b = def ( c = efg ) ) )
> B = (abc)
> C = (
> (a = abc)
> (f = ghi)
> )
>
> ..etc. (basically each entry can be split over multiple lines, and have an
> arbitrary number of parameters enclosed within parentheses). I would like to
> be able to match the beginning of each entry, e.g. parsing the above file
> results in
>
> A
> B
> C
>
> But I'm not sure how to manage the arbitrary number of ( and ) for each
> entry. If I was doing this in C, I could use a pushdown stack to to track
> the number of parentheses. Is there an elegant way to do this in Perl? (I've
> found more elegant ways to do things in Perl than I ever thought possible
> with a single language, but this one has eluded me so far.. :-))
>
> Cheers!
>
> Rory
>
> --------------------------------------------
> Rory Winston,
> Developer,
> OneLine Team,
> TeleWest Communications,
> Genesis Business Park,
> Albert Drive,
> Woking Surrey,
> GU21 5RW
>
> mailto: rory.winston at domain telewest.co.uk
> phone: 01483 295 653
>
> ------------------------------------------------------------------------------
> Live Life in Broadband
> www.telewest.co.uk
>
>
> The information transmitted is intended only for the person or entity to which it is addressed and may contain confidential and/or privileged material.
> Statements and opinions expressed in this e-mail may not represent those of the company. Any review, retransmission, dissemination or other use of, or taking of any action in reliance upon, this information by persons or entities other than the intended recipient is prohibited. If you received this in error, please contact the sender immediately and delete the material from any computer.
>
>
> ==============================================================================
>
>
> --
> Irish Linux Users' Group: ilug at domain linux.ie
> http://www.linux.ie/mailman/listinfo/ilug for (un)subscription information.
> List maintainer: listmaster at domain linux.ie

-- 


This archive was generated by hypermail 2.1.6 : Thu 06 Feb 2003 - 13:12:46 GMT