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:
- <?php
- echo "Preg_replace used time:";
- $start = time();
- for($i=1;$i<=100000;$i++) {
- $str = "ssssssssssssssssssssssssssss";
- preg_replace("/s/","",$str);
- }
- $ended = time()-$start;
- echo $ended;
- echo "
- ereg_replace used time:";
- $start = time();
- for($i=1;$i<=100000;$i++) {
- $str = "ssssssssssssssssssssssssssss";
- ereg_replace("s","",$str);
- }
- $ended = time()-$start;
- echo $ended;
- echo "
- str_replace used time:";
- $start = time();
- for($i=1;$i<=100000;$i++) {
- $str = "sssssssssssssssssssssssssssss";
- str_replace("s","",$str);
- }
- $ended = time()-$start;
- echo $ended;
- ?>
Example result of Ereg Regular Expression in PHP:
- Preg_replace used time:5
- ereg_replace used time:15
- 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.