What is the difference between reg_match and preg_match?
Both seem to match with regular expressions, but I can't decide which one to use.
If it's almost the same function, which one should I use for now?
php regular-expression
To begin with, I use PCRE regular expressions such as preg_match
, and only for other (non-UTF-8 multibyte characters), I use mbegex regular expressions such as mb_ereg
.
The reasons are as follows:
First, PHP has three regular expression groups.
For each, the following is true:
Regular expressions provided in the PCRE (Perl-Compatible Regular Expressions) module, and are compatible regular expressions in the Perl language.
You can use the u
switch to treat it as a UTF-8 string.
Also, a delimiter is required, and .
does not match a new line by default.
$str='abcdefghijklmnopqrstuvwxyz';
var_dump(preg_match('/h.+y/',$str)));
// output:1 (matching)
$str="abcdefghijklmn\n"
. "opqrstuvwxyz";
var_dump(preg_match('/h.+y/',$str)));
// output:0 (not matched, new line does not match .)
The PHP documentation also details the modifiers and regular expression syntax that you can use for patterns.
The POSIX extended regular expression provided on the POSIX regex module as defined in POSIX 1003.2.
It is deprecated in PHP 5.3 or later and is not binary safe, so you should avoid using it.
http://php.net/manual/ja/intro.regex.php
mbstring Regular expression for multi-byte characters in the module.
Act as the character encoding specified in mbstring.internal_encoding
or mb_regex_encoding()
in php.ini
. (Note that the character encoding of mb_ereg is not changed when specified in mb_internal_encoding()
.
Also, no delimiter is required, and .
matches a new line by default.
$str='abcdefghijklmnopqrstuvwxyz';
var_dump(mb_ereg('h.+y',$str)));
// output:1 (matching)
$str="abcdefghijklmn\n"
. "opqrstuvwxyz";
var_dump(mb_ereg('h.+y',$str)));
// output:1 (matching, line feed also matching .)
mb_ereg_match
determines whether the string matches from the beginning (similar to preg_match
), and uses mb_ereg
to match even in the middle of the string.
$str='abcdefghijklmnopqrstuvwxyz';
var_dump(mb_ereg_match('a',$str));
// output —true (matching)
$str = 'abcdefghijklmnopqrstuvwxyz';
var_dump(mb_ereg_match('g',$str));
// output —false (because it does not match, does not start with g)
$str = 'abcdefghijklmnopqrstuvwxyz';
var_dump(mb_ereg_match('.+g',$str));
// output —true (matching)
$str = 'abcdefghijklmnopqrstuvwxyz';
var_dump(mb_ereg('g',$str));
// output:1 (matching)
Assume that ereg_match
means ereg
.
The differences are summarized in PHP: Differences from POSIX regular expressions - Manual // Avoid full text citations because they are long
The biggest difference is that the preg_match
function requires a delimiter?
The following is an indicator of which to use, but in ereg
function reference,
Warning This function is deprecated in PHP 5.3.0. We strongly recommend that you do not use this feature.
and
Hintereg() is deprecated in PHP 5.3.0 and later.We recommend that you use preg_match() instead of this function.
and so on.
Therefore, it is better to use the preg_match
function.
ereg_match=mb_ereg_match
If you are asking this question with the intention of doing so, the key is whether or not to handle multibyte strings.
It seems that they are trying various things at the link below.
http://www.nilab.info/z3/20140522_01_php_regex.html
© 2024 OneMinuteCode. All rights reserved.