I'm processing a string with embedded newlines. For testing I was
storing the text in __DATA__ and slurping it into a string. This works
fine. However when I read in a file, I'm having trouble with the line
endings. Matching begining/end of logical lines is not working as I
expect. Regexes like the one below match when using the DATA filehandle,
but don't when opening other text files on my Mac.

    $text =~ s/^Text to match.*$//m;

Is this due to UNIX '\n' vs. Mac '\r' line endings? I assumed the 'm'
modifier would recognize any line ending.

Oh what to do?

You have several possibilities, depending on what you are trying to do.

You could explicitly use either line ending, as it:

$text =~ s/(\012|\015|\A)Text to match[^\012\015]*(\012|\015|\z)/$1$2/;

or using backward/forward assertions:

$text =~ s/(?:\A|(?<=\012|\015))Text to match[^\012\015]*(?=\012|\015|\z)//;

(the convoluted backward assertion is required because backward assertions must be fixed lengths)

Or you could convert $text to \n line endings:

$text =~ s/(\015\012|\012|\015)/\n/g;
$text =~ s/^Text to match.*$//m;

Or you could detect the line ending and explicitly use it.

Enjoy,
   Peter.

--
Check out Interarchy 8.1.1, just released, now with Amazon S3 support.
<http://www.stairways.com/>  <http://download.stairways.com/>

Reply via email to