Perl split() question (OT)...

Steve Bertrand iaccounts at ibctech.ca
Fri Jul 23 09:42:21 PDT 2004


> On Fri, 23 Jul 2004, Steve Bertrand wrote:
>
>> Perl hackers -- Figured someone would have a reasonably quick, easy
>> answer
>> for this:
>>
>> I am trying to read through a file, line-by-line, and I want to extract
>> the text in between the [ and ] characters.
>
> This is a job for......capturing parens!!!
>
> Try this:
>
> if ($_=~/\[(.+)\]/) {
> 	$var=$1;
> }
>
> $1 would be the string matched by the regex between ( and )
>

Absolutely perfect!! This worked excellent:

while ($_ = <LOGFILE>) {
        if ($_ =~ /$struct/ && $_ =~ /$structStart/) {
                if ($_ =~ /\[(.+)\]/) {
                        $string = $1;
                        print "$string -- $struct$structStart\n";
                        # ... do other stuff, snipped
                }
        } else {

Thank-you so much!

Steve

>
>
> 			Fer
>




More information about the freebsd-questions mailing list