主页  »  技术支持  »  指南/使用技巧  »  UltraEdit  »  Perl regex tutorial: non-greedy regular expressions
IDM Power Tips

Perl regex tutorial: non-greedy expressions

Have you ever built a complex Perl-style regular expression, only to find that it matches much more data than you anticipated? If you've ever found yourself pulling your hair out trying to build the perfect regular expression to match the least amount of data possible, then non-greedy Perl regular expressions are what you need.

By default, Perl-style regular expressions are "greedy". This means they will match as much data as possible before a line break. Even if the conditions of the regular expression have been met, but a line break has not yet occurred, the regular expression will continue searching for data that satisfies the search criteria.

By using "non-greedy" Perl-style regular expressions, you can prevent this from occuring and stop the search as soon as the search criteria has been satisfied. Read on to find out how this unique feature of Perl-style regular expressions can save you time and frustration!

For more information on Perl-style regular expressions, visit our power tip on this subject.

Non-Greedy Perl Regular Expressions

Typically, when using Perl-style regular expressions to match strings of data, normal Perl-style regular expression syntax will match as much data as possible. For example, if you want to search for an HTML hyperlink using the following Perl-style regular expression:

<a href=".*</a>

On the following source code:

Click to visit <a href="http://www.ultraedit.com">UltraEdit.com</a>. Click to visit <a href="http://www.ultracompare.com">UltraCompare.com</a>.

Everything from the first "<a href..." to the last "</a>" on the same line (as highlighted in red) will be matched by the regular expression. This is undesirable as the purpose of the regular expression is to match one hyperlink at a time, whereas this regular expression is matching two hyperlinks and the normal text between on the same line.

Non-Greedy Perl Regular Expressions

This is where non-greedy regular expressions are useful. To use non-greedy Perl-style regular expressions, the "?" (question mark) may be added to the syntax, usually where the wildcard expression is used.

In our above example, our wildcard character is the ".*" (dot and star). The dot will match any character except a null (hex 00) or new line. The star will match the previous character zero or more times. So a dot followed by a star in Perl-style syntax literally means match any character zero or more times.

To add in the non-greedy operator, we simply need to add a "?" to the end of our wildcard operators. So, our new, non-greedy regular expression would look like this:

<a href=".*?

Our non-greedy "?" operator now tells the regular expression engine to match as little data as possible. As soon as all conditions of the regular expression have been met, the search will end. So now using our above example, only the highlighted text below would be matched:

Click to visit <a href="http://www.ultraedit.com">UltraEdit.com</a>. Click to visit <a href="http://www.ultracompare.com">UltraCompare.com</a>.

Non-Greedy Perl Regular Expressions

As you can see from our above example, using non-greedy Perl-style regular expressions can preven much heartache when doing search and replace functions on HTML, XML, PHP, and virtually any other file where matched data must be limited.


Back to power tips index


Buy Now
Upgrade
Download

Cnet/Download.com review
"我们并没有夸大其词:
UltraEdit真的非常棒..."

Corporate Licensing

It's easy to purchase corporate licensing from us!

  • Bulk/quantity discounts
  • Enterprise/plant/site licensing
  • Purchase with PO or invoice

The new UltraSuite

The only fully integrated solution of its kind!

Four powerful applications: UltraEdit, UltraCompare, UltraFinder, and UltraSentry!

Save over 33% when you bundle with the UltraSuite!

Power of Portable

Now supporting PortableApps!

UE/UC Mobile

Unleash the power of the total file management solution on your flash drive!