[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: [OT] perl regex problem



On Mon, Feb 05, 2001 at 06:31:03PM -0800, Hunter Marshall wrote:
> I am a long time debian and perl user. But obviously long enough!
> Forgive the slight misuse of the list, but can anyone shed light
> on what I'm doing wrong in this attempt to find \n\n in a text file 
> with perl? I'm sure I've done this before.

Hi,

depending on what exactly you want to do, it might help to set the
input record separator ($/) to something other than the default "\n".
If you set it to "\n\n" for example, the input "lines" you get will
be broken up at double-newlines...

#!/usr/bin/perl

$/="\n\n";  # set input record separator

while (<>) {
    # every line ($_) will inlcude everything up to and including
    # the "\n\n", so you can match against it (if still needed...)
    if (/\n\n/) {
        print "yup\n";
    }
}

The input record separator can be any string, but it's _not_ interpreted
as a regular expression. If you need that, you can read the whole file
into a string in one go (if it fits into memory), and then do any kind
of processing on it, e.g. split() or repeated regex matching

#!/usr/bin/perl

$/=undef;   # 'undef' causes whole file to be read in 

$s = <>;    # file now in $s

while ($s =~ /\n\n/g) {   # do some repeated matching
    print "yup\n";
}

The advantage of the latter approach is that you can craft your
regex to do any kind of sophisticated matching, independently of some
concept of line termination...

BTW, if you don't want the modification of the input record separator
to apply globally for the script, you can use "local $/ =..." instead,
in soubroutines, for example ("my" doesn't work here).

Cheers,
Erdmut



-- 
Erdmut Pfeifer
science+computing gmbh

-- Bugs come in through open windows. Keep Windows shut! --



Reply to: