Perl print between 2 patterns

I have been unable to find this anywhere; I have a multiline variable, and I want to print the text between two patterns in that variable. So the variable is

My
real
name
is
not
Deadman

And I need the output to be this, by printing between "real" and "not"

name
is

or including the two patterns; doesn't matter.

The only ways I've seen of doing this don't involve searching a variable, only hardcoded __DATA__ or files

EDIT: by the way, sorry if I didn't make it clear, I want it all in perl, it's a perl script, not using other languages.

A string is a string, despite newlines, carriage returns, and sometimes nulls, never mind multicharacter like eucjis AND utf8.

I thought trimming a string in PER using regex was simple, let me google it.

Yep, like inline sed: The world is amazing: Perl: Trim a string

Then print the trimmed string.

If I understand correctly, what that is doing is removing what you tell it to remove, am I right? If I am right, I can't use that, the damn file is 2149 lines, and it always changes.

If that's not what it's doing, can you show me how it would work with my example text?

Well, a sed-ism for your case would be:

s/.*pattern1\(.*\)pattern2.*/\1/

but I do not play in PERL, being a sed or C or ksh guy. PERL may treat the string buffer as one line, or many, regardless of embedded characters. That is the 'like one line' solution, my first guess. Write a simple like the web site and try it out with a simple string that models your case, but simpler. The best knowledge is locally made.

Well, sadly, it doesn't seem to work. I used some test text with the trim thing you linked to, did not work with newlines. look:

sub trim{
   my $string = shift;
   $string =~ s/.*real\(.*\)not.*/\1/g;
   return $string;
}
print "[".trim("My real name is not Deadman")."]\n";

And the output was

[name is]

Which worked perfectly. Sadly, next try:

sub trim{
   my $string = shift;
   $string =~ s/.*real\(.*\)not.*/\1/g;
   return $string;
}
print "[".trim("My\nreal\nname\nis\nnot\nDeadman")."]\n";

And the output was

[My
real
name
is
not
Deadman]

And just in case that was a special case, I tried it with what I needed, again, printed the entire thing.

How about this

 
perl -0pe 's/.*(real.*not\n).*/\1/sg' input

2 things - I wouldn't know how to go about including that in ONLY A PERL SCRIPT and the input is a variable, not a file.

Hi.

Using getmmg code, modifying and comparing:

#!/usr/bin/env perl

# @(#) p1	Demonstrate feature (minimal).

use strict;
use warnings;
my ( $s1, $s2, $original );

print " Hello, world from perl minimal.\n";

$original = "My\nreal\nname\nis\nnot\nDeadman";
$s1       = $original;
print "[" . trim1($s1) . "]\n";

$s2 = $original;
print "[" . trim2($s2) . "]\n";

sub trim1 {
  my $string = shift;

  # $string =~ s/.*real\(.*\)not.*/\1/g;
  $string =~ s/.*real(.*)not.*/$1/g;
  return $string;
}

sub trim2 {
  my $string = shift;

  # $string =~ s/.*real\(.*\)not.*/\1/sg;
  $string =~ s/.*real(.*)not.*/$1/sg;
  return $string;
}

# print "[".trim("My\nreal\nname\nis\nnot\nDeadman")."]\n";

exit(0);

producing:

% ./p1
 Hello, world from perl minimal.
[My
real
name
is
not
Deadman]
[
name
is
]

Best wishes ... cheers, drl

Well, you are right that newlines are not considered . food in PERL like this. You could pull the lines one at a time, look for the first pattern, trim it, start printing and looking for the second pattern, when you find it, trim, print and stop, but that seems harder than should be necessary. Maybe there is a way to get the right substring in one fell swoop.

Else, you could shell out to sed, which is cheating.

This looks close: Red Antigua - Perl trim function found with trim string PERL lines - Google Search

This would work if we had the offset: Perl substr Function

It looks like index can tell us the offset: Perl String Functions

Damn, I am learning PERL.

drl, that works perfectly, thank you.

More code tha something like: a=index string A in string X; b=index string B in string X; substr( X, a + length A, b - a - length A )