gregor-e has asked for the wisdom of the Perl Monks concerning the following question:

I need to match a multi-line pattern with minimal preceeding greed. For example, I'd like:
$source = "one\ntwo\none\nthree"; $source =~ m/(one.*?ee)/s;
to end up with $1 eq "one\nthree". Instead, it ends up that $1 eq "one\ntwo\none\nthree" How do I minimize preceeding greed?

Replies are listed 'Best First'.
Re: minimal greed
by chromatic (Archbishop) on Feb 15, 2000 at 03:19 UTC
    What your regex asks for is:
    o followed by n followed by e, followed by any character (including a newline -- /s switch) repeated zero or more times (but as few as possible) followed by e followed by e and saved in $1.
    What you'll need to do is use multiple regexes, or multiple captures within your regex:
    m/(one).*?(\s\w+ee)\b/s; That is, match: o followed by n followed by e (captured in $1) any character (including newline -- /s switch) repeated zero or more times (as few as possible while still allowing a match) followed by a whitespace character (\s) followed by a word character (alphanumeric) repeated at least one time followed by e followed by e (captured in $2) followed by a word boundary
    It's closer to what you want, though it uses $1 and $2. japhy is right -- your question is a little vague.
Re: minimal greed
by japhy (Canon) on Feb 14, 2000 at 08:52 UTC
    Well, that's a pretty vague question. Do you want to match "one followed by any non-'one' strings, until the first 'ee'" or something more general, like the shortest non-greedy match?