Analyzes the regular expression Ereg in PHP. We know that Perl is compatible with regular expressions in PHP. what do we need to know about Ereg regular expressions? Here, we use the difference analysis between Perl-compatible regular expressions and PerlEreg to 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:
-
- 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.
Why? Here we will analyze the differences between Perl-compatible regular expressions and Perl/Ereg...