I'm trying to extract a record from inside a multi-line scalar using a single regex, for example:
#! /usr/local/bin/perl
my $source = "
name: Schwank E. Schwagg
address: 123 Dirtweed Dr.
user: 1
name: Bizzi Buddi
address: 321 Grapevine Way
user: 2
name: Fernal Brimstone
address: 666 Lucifer Ln
user: 3
";
$source =~ m/.*?(name.*?user: 2)/s;
print "$1\n";
I'd like this example to extract the name/address record for user #2, Bizzi Buddi, so I attempt to grab everything from "name" to "user: 2". Unfortunately, this regex is not minimally greedy, despite the .*?, and ends up matching the preceeding record also, which contains "name".
How can I create a regex that minimally matches /literal_1.*literal_2/ and guarantees that the first literal is not repeated within the wildcard?