Discrimination of Ereg Regular Expressions in PHP

Source: Internet
Author: User
Tags ereg preg

We know that Perl is compatible with regular expressions in PHP. What do we need to know about Ereg regular expressions? Here we will introduce Ereg regular expressions to you through the Difference Analysis Between Perl-Compatible Regular Expressions and Perl/Ereg, hoping to help you.

Although it is called "Perl Compatible Regular Expressions", PHP is different from Perl's regular expressions. For example, the modifier "G" indicates all matches in Perl, however, this modifier is not supported in PHP.

Analysis of Ereg Regular Expressions in PHP:

There is also the difference with the ereg series functions. ereg is also a regular expression function provided in PHP, but it is much weaker than preg.

1. ereg does not need or use delimiters and modifiers. Therefore, ereg is much weaker than preg.

2. About ".": in a regular expression, all characters except line breaks are generally entered, but "." In ereg is any character, that is, line breaks! If you want "." To include line breaks in the preg, you can add "s" to the modifier ".

3. ereg uses greedy mode by default and cannot be modified. This causes a lot of trouble for replacement and matching.

4. Speed: this may be a concern of many people. Will the preg feature be powerful in exchange for speed? Don't worry, the preg speed is much faster than ereg. I did a program test:

In PHP, The Ereg regular expression time test instance:

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

Example result of Ereg Regular Expression in PHP:

 
 
  1. Preg_replace used time:5   
  2. ereg_replace used time:15   
  3. str_replace used time:2  

Str_replace is faster than ereg_replace because it does not need to be matched.

The related content of Ereg Regular Expressions in PHP will be introduced here. I hope you can understand and learn Ereg Regular Expressions in PHP.


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.