-
Notifications
You must be signed in to change notification settings - Fork 160
Regex (PCRE)
Kirill Nesmeyanov edited this page May 12, 2015
·
2 revisions
As you know, Zend PHP has the PCRE extension for working with regular expressions, it includes the all preg_* functions
. JPHP does not support these functions and PCRE for regexp. There is no implementation of this written on Java. Usage of the PCRE native library is not a good approach for the JVM Stack so JPHP has own regex library that is based on Java regex library.
Use the php\util\Regex
class to replace the PCRE functions. It's an immutable class.
use php\util\Regex;
if (Regex::match('^[a-z]+$', $str, Regex::CASE_INSENSITIVE)) { }
// instead of
if (preg_match('/^[a-z]+$/i', $str) { }
preg_match_all
- You can replace
preg_match_all
to theRegex
iterations.
$regex = Regex::of('([0-9]+)');
foreach ($regex->with('a 12 b 34 c 384') as $number) {
var_dump($number);
}
preg_replace
- To replace via regex, you can use
Regex::replace
andRegex::replaceFirst
methods.
$regex = Regex::of('([0-9]+)');
$string = $regex->with('a 12 b 34 c 384')->replace('NUM'); // result will be "a NUM b NUM c NUM"
preg_replace_callback
- Use
Regex::replaceWithCallback
andRegex::group
.
$regex = Regex::of('([0-9]+)');
$string = $regex->with('a 12 b 32 c 384')->replaceWithCallback(function(Regex $self) {
return $self->group() * 2;
});
// string will be "a 24 b 64 c 768"
preg_quote
- Use
Regex::quote
orRegex::quoteReplacement
forreplaceWithCallback
.
JPHP Group 2015