" The love that lasts longest is the love that is never returned. "  —  W. Somerset Maugham
 

Greedy and Non-Greedy Matching using Perl Regular Expression

more...

In this article I will show the difference between the default greedy (.+ and .*) and non-greedy matching using Perl-compatible regular expression.

For example, I have a string in a line:

$line= "Seq[03] Command : CREATE("A") Seq[04] Command : CREATE("B") Seq[04] error: 5006 Seq[03] error: 5006 Seq[05] Command : DELETE("A") Seq[05] error: 5006 ";

And I could capture all the Command without the error code using the following regular expression:

m/Seq\[[0-9]{2}\].+Command.+\(.+\)/gi

and the Perl code to show all string matching string:

while( $line =~  m/Seq\[[0-9]{2}\].+Command.+\(.+\)/gi ) {
       print $& . "\n";
}

But it doesn’t work as I expected because of the “greediness” of .+ and/or .*. The above code will match:

Seq[03] Command : CREATE("A") Seq[04] Command :
CREATE("B") Seq[04] error: 5006 Seq[03] error: 5006 Seq[05] Command : DELETE("A")

I expect the following output:

Seq[03] Command : CREATE("A")
Seq[03] Command : CREATE("B")
Seq[03] Command : DELETE("A")

To get the matching output as I expected, I need to modify the greedines the regular expression to a non-greedy one. How? After .+ and/or .* you need to add a question mark ? so the regular expression is now become:

m/Seq\[[0-9]{2}\].+?Command.+?\(.+?\)/gi

The following code is the full Perl code:

#!/bin/perl
$line= "Seq[03] Command : CREATE("A") Seq[04] Command : CREATE("B") Seq[04] error: 5006 Seq[03] error: 5006 Seq[05] Command : DELETE("A") Seq[05] error: 5006 ";
while( $line =~  m/Seq\[[0-9]{2}\].+?Command.+?\(.+?\)/gi ) {
       print $& . "\n";
}
10
Share up your minds and leave a comment
  1. The Record…

    I think you write superbly, wish I could write this well. Thanks….

  2. Layer…

    I have to agree with this post, well said….

  3. Charlie Brown…

    Excellent post, you could almost quote this…

  4. Twizzle…

    Great writing style, love the post…

  5. Evenlope…

    Superb post, 10 out of 10 from me…

  6. The Fishman…

    This reminds me of another post, both were really good….

  7. echodelta…

    Probably one of the finest articles, in the world…

  8. fivealive…

    Love the post truly….

  9. email marketing reviews…

    Superb post, I like how you’ve structured it…

  10. automatic email responder…

    Great post, bookmarked…

Comment Form
XHTML Expert!
You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

* = required fields