PHP code example of yoeunes / regex-parser
1. Go to this page and download the library: Download yoeunes/regex-parser library. Choose the download type require. 2. Extract the ZIP file and open the index.php. 3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
yoeunes / regex-parser example snippets
use RegexParser\Regex;
use RegexParser\ReDoS\ReDoSMode;
$regex = Regex::create([
'runtime_pcre_validation' => true,
]);
// Parse a pattern into AST
$ast = $regex->parse('/^hello world$/i');
// Validate pattern syntax and semantics
$result = $regex->validate('/(?<=test)foo/');
if (!$result->isValid) {
echo $result->error;
}
// Check for ReDoS risk (theoretical by default)
$analysis = $regex->redos('/(a+)+$/');
echo $analysis->severity->value; // 'critical', 'safe', etc.
// Optional: attempt bounded confirmation
$confirmed = $regex->redos('/(a+)+$/', mode: ReDoSMode::CONFIRMED);
echo $confirmed->isConfirmed() ? 'confirmed' : 'theoretical';
// Get human-readable explanation
echo $regex->explain('/\d{4}-\d{2}-\d{2}/');