Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

minimal greed

by gregor-e (Beadle)
on Feb 12, 2000 at 02:11 UTC ( [id://3405]=perlquestion: print w/replies, xml ) Need Help??

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?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://3405]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (4)
As of 2024-04-24 18:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found