maj12 has asked for the wisdom of the Perl Monks concerning the following question:

OK - 3 days exp. with perl here so go easy... ;-)
Let say I have a text file like so:

logging {
category lame-servers { null; };
category cname { null; };
};

zone "." in {
type hint;
file "root.cache";
};

zone "127.IN-ADDR.ARPA" {
type slave;
file "127";
masters { 10.10.1.113; };
};

Returns:

logging { category lame-servers { null; };
category cname { null; };
zone "." in { type hint; file "root.cache";};
zone "127.IN-ADDR.ARPA" { type slave; file "127"; masters { 10.10.1.113; };

It's splitting when it finds "{}"'s embedded in "{}". Now, I want to break it up into 3 logical "sections". I can get it to split using this regex:

foreach my $lines(@temparray) #where temparray is the above { chomp $lines; $string=$string.$lines; } while($string=~/(\w+ )([\w\W\d]+?\};)+?/g) { print "$1$2<br>"; }

but I can't get it to recognize line returns like this:

while($string=~/(\w+ )([\w\W\d]+?\};\n)+?/gm)

What gives?

Replies are listed 'Best First'.
Re: multiline, global regex
by jeroenes (Priest) on Oct 18, 2001 at 12:03 UTC
    Apart from the chomp thing, there are a few things that can be done more efficiently. If you are new on perl, try reading up on online docs, perldoc, and get accostumed to search.cpan.org (CPAN is your friend).

    Try to use join, it is more convenient than a loop:

    my $string = join '', @temparray; # or even <FILE>
    Why not use Parse:RecDescent for the parsing?

    Otherwise, you can trim some delimiters down, as you do not really need the closing brackets.

    use SuperSplit; $string =~ tr/}//d; $nested_list = supersplit( ';','{',' ','\n','\n\n', $string );
    Does a decent try at breaking it down, although the code above is a bit rough. RecDescent would be better. Take a look at perlref, perldsc, perllol and perlreftut, you can find a link of the latter in my homenode.

    This post would be longer, but I have to celebrate now...

    Jeroen
    "We are not alone"(FZ)

    Update: forgot to add the whiteline

Re: multiline, global regex
by drinkd (Pilgrim) on Oct 18, 2001 at 04:04 UTC
    The line chomp $lines takes the \n off the end of the string. Try it without chomping if you need to use the line feeds to split on. drinkd
Re: multiline, global regex
by maj12 (Novice) on Oct 18, 2001 at 16:35 UTC
    Excellent. Thanks for the head-start, folks. Maj12 "When in doubt; empty your magazine."