Categories

Tags Cloud

RSS Feed

Subscribe to blog RSS Feed Subscribe to Blog's RSS Feed
JUN
2009
29
0 Comments
164 Hits
Greedy and Non-Greedy Matching using Perl Regular Expression
Posted under: Tutorial
Tags: Scripting

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 single 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 ";

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";
}

 

Next
Prev

Write Your Comments

Comments are parsed with Markdown.
 
Notes
* Your email is required to submit this form, and it will not be published or shared without your consent. We use your email address to show your avatar picture profile from Gravatar. Don't have one? Then sign up to gravatar and create your own here.
We also filters your comment against SPAM because we hate SPAM as much as you do. If your comment is recognized as SPAM then it will be moderated, otherwise it will shows up immediately.
Form Key: #a9cdd3a4c1fe2db985d0961b0bfd1adb
Loading...