An analysis of ereg regular expressions in PHP _php tutorial

Source: Internet
Author: User
Tags ereg preg vars
We know Perl-compatible regular expressions in PHP, so what do we need to know about Ereg regular expressions? Here we introduce you to the Ereg regular expressions by using Perl-compatible regular expressions and perl/ereg differential analysis, and hope to help you.

Although called "Perl-compatible regular Expressions", PHP is still a bit different from Perl's regular expressions, such as the modifier "G", which represents all matches in Perl, but does not include support for this modifier in PHP.

An analysis of ereg regular expressions in PHP:

There is the difference between the Ereg series functions, Ereg is also the regular expression function provided in PHP, but compared with preg, it is much weaker.

1, Ereg inside is not necessary and can not use separators and modifiers, so ereg function than preg to a lot weaker.

2, about "." : The point in the regular is usually all characters except the newline character, but in the Ereg "." Is any character, which includes line breaks! If in Preg hope "." To include line breaks, you can add "s" to the modifier.

3, ereg default use greedy mode, and can not be modified, this gives a lot of replacement and matching trouble.

4, Speed: This may be a lot of people are concerned about the problem, will not preg powerful is to exchange speed for? Do not worry, preg speed is far faster than Ereg, the author has done a program test:

Ereg Regular expression time test instance in PHP:

 
 
  1. Echo "Preg_replace used time:";
  2. $start = time ();
  3. for ($i=1; $i <=100000; $i ++) {
  4. $str = "Ssssssssssssssssssssssssssss" ;
  5. preg_replace ("/s/","",$str);
  6. }
  7. $ended = Time ()- $start ;
  8. Echo $ended;
  9. Echo "
  10. ereg_replace used time: ";
  11. $start = time ();
  12. for ($i=1; $i <=100000; $i ++) {
  13. $str = "Ssssssssssssssssssssssssssss" ;
  14. ereg_replace ("s","",$str);
  15. }
  16. $ended = Time ()- $start ;
  17. Echo $ended;
  18. Echo "
  19. Str_replace used time: ";
  20. $start = time ();
  21. for ($i=1; $i <=100000; $i ++) {
  22. $str = "Sssssssssssssssssssssssssssss" ;
  23. Str_replace ("s","",$str);
  24. }
  25. $ended = Time ()- $start ;
  26. Echo $ended;
  27. ?>

Ereg Regular Expression instance results in PHP:

 
  
  
  1. Preg_replace used Time:5
  2. ereg_replace used time:15
  3. Str_replace used Time:2

Str_replace because there is no need to match so very fast, and preg_replace faster than ereg_replace to a lot faster.

The contents of Ereg regular expressions in PHP are introduced here, which hopefully helps you understand and learn about Ereg regular expressions in PHP.


http://www.bkjia.com/PHPjc/446583.html www.bkjia.com true http://www.bkjia.com/PHPjc/446583.html techarticle We know Perl-compatible regular expressions in PHP, so what do we need to know about Ereg regular expressions? Here we are using Perl-compatible regular expressions and Perl/ereg to analyze the difference between ...

  • 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.