Introduction to greedy and non-Greedy expressions in php regular expressions, and php Regular Expressions

Source: Internet
Author: User
Tags ereg

Introduction to greedy and non-Greedy expressions in php regular expressions, and php Regular Expressions

I. Greedy and non-greedy

What is greedy? For example, if you want to eat <td> bread 1 </td> <td> bread 2 </td> from a string, you can eat only one bread, but you are greedy, so we took out the two breads In the first <td> to the last </td>. If you want to eat more, you are not greedy, that is, you are not greedy, eat only one loaf of bread.

Let's see how greedy the regular expression is.

<? Php $ str = '<td> bread 1 </td> <td> bread 2 </td>'; preg_match ('/<td> (. *) <\/td>/', $ str, $ res); print_r ($ res );

Result:

Array
(
[0] => <td> bread 1 </td> <td> bread 2 </td>
[1] => bread 1 </td> <td> bread 2
)

0 indicates the entire character, and 1 indicates the first match.

How can we limit greed?

<? Php $ str = '<td> bread 1 </td> <td> bread 2 </td>'; preg_match ('/<td> (.*?) <\/Td>/', $ str, $ res); print_r ($ res ); array ([0] => <td> bread 1 </td> [1] => bread 1)

Add "? ", The number of matches can be as few as possible.

PHP can also be implemented through modifiers,

<? Php $ str = '<td> bread 1 </td> <td> bread 2 </td>'; preg_match ('/<td> (. *) <\/td>/U', $ str, $ res); print_r ($ res );

The result is the same as above. This is the role of the modifier U.

Ii. Pre-Search

Pre-search is a non-get match and is not stored for future use.

1. Forward pre-search "(? = Xxxxx )","(?! Xxxxx )"

"(? = Xxxxx) ": the right side of the gap must be able to match the expression in the section xxxxx,

<?php$str = 'windows NT windows 2003 windows xp';preg_match('/windows (?=xp)/',$str,$res);print_r($res);

Result:

Array
(
[0] => windows
)

This is windows before xp, and does not take the front of NT and 2003.

Format :"(?! Xxxxx) ", the right side of the gap must not match the expression of xxxxx

<?php$str = 'windows NT windows 2003 windows xp';preg_match_all('/windows (?!xp)/',$str,$res);print_r($res);

Result:

Array
(
[0] => Array
(
[0] => in windows, this is before nt.
[1] => windows is before 2003
)

)

From this we can see that pre-search is not stored for future use.

The storage of the participants is compared.

<?php$str = 'windows NT windows 2003 windows xp';preg_match_all('/windows ([^xp])/',$str,$res);print_r($res);

Result:

Array
(
[0] => Array that matches all modes

(
[0] => windows N
[1] => windows 2
)

[1] => Array consisting of strings matched by the Array sub-pattern, obtained through storage.
(
[0] => N
[1] => 2
)

)

2. Reverse pre-search "(? <= Xxxxx )","(? <! Xxxxx )"

"(? <= Xxxxx) ": the" Left "of the gap can match the xxxxx part.

<?php$str = '1234567890123456';preg_match('/(?<=\d{4})\d+(?=\d{4})/',$str,$res);print_r($res);

Result:

Array
(
[0] = & gt; 56789012
)

Match the eight digits in the middle except the first four digits and the last four digits.
"(? <! Xxxxx) ": the" Left "of the gap cannot match the xxxx part.

<? Php $ str = 'I'; preg_match ('/(? <! Me) \ d +/', $ str, $ res); print_r ($ res );

Result:
Array
(
[0] = & gt; 234567890123456
)

 
Iii. Differences between preg and ereg


PHP uses two sets of regular expression rules at the same time. One set is POSIX Extended 1003.2 compatible with regular expressions developed by the Institute of Electrical and Electronics Engineers (IEEE) (in fact, PHP does not fully support this standard ), the other set comes from the PCRE (Perl Compatible Regular Expression) library to provide PERL-Compatible Regular Expressions. PHP5.3 POSIX has been deleted.

Preg_match is more efficient than ereg.

Articles you may be interested in:
  • Non-Greedy pattern matching in php Regular Expressions
  • [Regular expression] greedy mode and non-Greedy Mode
  • Small discussion on efficiency greedy, non-greedy, and backtracking of Regular Expressions
  • Greedy and non-Greedy pattern in regular expressions (Overview)
  • Usage of non-Greedy pattern matching in php Regular Expressions
  • Greedy and inert matching of Java Regular Expressions
  • Regular Expression (regex) greedy mode and lazy Mode

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.