InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

Menu
  • Home
  • Sitemap

Python Programming Language Best Tutorials and Code Examples

Learn Python Right Now!
Home
PHP
Choosing Greedy or Nongreedy Matches in PHP
PHP

Choosing Greedy or Nongreedy Matches in PHP

InfinityCoder December 24, 2016

You want your pattern to match the smallest possible string instead of the largest.

Place a ? after a quantifier to alter that portion of the pattern, as in Example 23-6.
Example 23-6. Making a quantifier match as few characters as possible

1
2
// find all <em>emphasized</em> sections
preg_match_all('@<em>.+?</em>@', $html, $matches);

Or use the U pattern-modifier ending to invert all quantifiers from greedy (“match as many characters as possible”) to nongreedy (“match as few characters as possible”).

The code in Example 23-7 does the same thing as the code in Example 23-6.
Example 23-7. Making a quantifier match as few characters as possible

1
2
// find all <em>emphasized</em> sections
preg_match_all('@<em>.+</em>@U', $html, $matches);

By default, all regular expression quantifiers in PHP are greedy. For example, consider the pattern <em>.</em>, which matches “<em>, one or more characters, </em>,” matching against the string I simply <em>love</em> your <em>work</em>.

A greedy regular expression finds one match, because after it matches the opening <em>, its .+ slurps up as much as possible, finally grinding to a halt at the final </em>.

The .+ matches love</em> your <em>work.
A nongreedy regular expression, on the other hand, finds a pair of matches. The first <em> is matched as before, but then .+ stops as soon as it can, only matching love.

A second match then goes ahead: the next .+ matches work. Example 23-8 shows the greedy and nongreedy patterns at work.
Example 23-8. Greedy versus nongreedy matching

1
2
3
4
5
6
7
8
9
10
$html = 'I simply <em>love</em> your <em>work</em>';
// Greedy
$matchCount = preg_match_all('@<em>.+</em>@', $html, $matches);
print "Greedy count: " . $matchCount . "\n";
// Nongreedy
$matchCount = preg_match_all('@<em>.+?</em>@', $html, $matches);
print "First non-greedy count: " . $matchCount . "\n";
// Nongreedy
$matchCount = preg_match_all('@<em>.+</em>@U', $html, $matches);
print "Second non-greedy count: " . $matchCount . "\n";

Example 23-8 prints:

1
2
3
Greedy count: 1
First non-greedy count: 2
Second non-greedy count: 2

Greedy matching is also known as maximal matching and nongreedy matching can be called minimal matching, because these methods match either the maximum or minimum number of characters possible.
The ereg() and ereg_replace() functions are always greedy. Being able to choose between greedy and nongreedy matching is another reason to use the PCRE functions instead.
Although nongreedy matching is useful for simplistic HTML parsing, it can break down if your markup isn’t 100 percent valid and there are, for example, stray <em> tags lying around.

2 If your goal is just to remove all (or some) HTML tags from a block of text, you’re better off not using a regular expression.

Instead, use the built-in function strip_tags(); it’s faster and it works correctly.
Finally, even though the idea of nongreedy matching comes from Perl, the U modifier is incompatible with Perl and is unique to PHP’s Perl-compatible regular expressions.
It inverts all quantifiers, turning them from greedy to nongreedy and also the reverse.
So to get a greedy quantifier inside of a pattern operating under a trailing /U, just add a ? to the end,the same way you would normally turn a greedy quantifier into a nongreedy one.

Share
Tweet
Email
Prev Article
Next Article

Related Articles

Preventing Session Fixation in PHP
You want to make sure that your application is not …

Preventing Session Fixation in PHP

Applying a Function to Each Element in an Array in PHP
You want to apply a function or method to each …

Applying a Function to Each Element in an Array in PHP

About The Author

InfinityCoder
InfinityCoder

Leave a Reply

Cancel reply

Recent Tutorials InfinityQuest

  • Adding New Features to bash Using Loadable Built-ins in bash
    Adding New Features to bash Using Loadable …
    June 27, 2017 0
  • Getting to the Bottom of Things in bash
    Getting to the Bottom of Things in …
    June 27, 2017 0

Recent Comments

    Categories

    • Bash
    • PHP
    • Python
    • Uncategorized

    InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

    About Us

    Start learning your desired programming language with InfinityQuest.com.

    On our website you can access any tutorial that you want with video and code examples.

    We are very happy and honored that InfinityQuest.com has been listed as a recommended learning website for students.

    Popular Tags

    binary data python CIDR convert string into datetime python create xml from dict python dictionary into xml python how to create xml with dict in Python how to write binary data in Python IP Address read binary data python tutorial string as date object python string to datetime python

    Archives

    • June 2017
    • April 2017
    • February 2017
    • January 2017
    • December 2016
    • November 2016
    Copyright © 2019 InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more
    Programming Tutorials | Sitemap