@ 2014-03-25T09:29:47Z <?php
function RC($flags = ""){
return new RegCode($flags);
}
class RegCodeFn {
public static function escape($s){
return preg_replace("/[\\/\\\\\\^\\[\\]\\.\\$\\{\\}\\*\\+\\(\\)\\|\\?\\<\\>]/", "\\\\$0", $s);
}
public static function compileArr($chain){
$l = count($chain);
$code = "";
for ($i=0; $i < $l; $i++) {
$code .= RegCodeFn::compile($chain[$i],($i + 1) < $l ? $chain[$i+1]['type'] == 'repeat' : false);
}
return $code;
}
public static function compile($item, $repeatNext){
switch ($item['type']) {
case 'range':
return '[' . ($item['not']?'^':'') . $item['allowed'] . ']';
break;
case 'group':
$res = RegCodeFn::compileArr($item['chain']);
return '(' . ($item['assertion']?$item['assertion']:'') . ($item['name']?(($item['assertion']?'':'?') . 'P<' . $item['name'] . '>' ):'') . $res['code'] . ')';
break;
case 'raw':
case 'text':
if($repeatNext && strlen($item['code']) > 1 && !(strlen($item['code']) == 2 && $item['code'][1] == '\\')){
return '(?:' . $item['code'] . ')';
}else{
return $item['code'];
}
break;
case 'any':
return ".";
break;
case 'end':
return '$';
break;
case 'start':
return '^';
break;
case 'repeat':
return $item['code'];
break;
}
}
}
class RegCode {
public $flags;
public $chain;
public function __construct($flags = ""){
if(is_string($flags)){
$this->flags = $flags?$flags:"";
}else{
$this->flags = $flags->flags;
$this->chain = clone $flags->chain;
}
}
public function start(){
$this->chain[] = array('type'=>"start");
return $this;
}
public function end(){
$this->chain[] = array('type'=>"end");
return $this;
}
public function range($allowed, $escape = true){
if($escape) $allowed = RegCodeFn::escape($allowed);
$this->chain[] = array(
'type'=>'range',
'not'=>false,
"allowed"=>$allowed
);
return $this;
}
public function notRange($allowed, $escape = true){
if($escape == null) $escape = true;
if($escape) $allowed = RegCodeFn::escape($allowed);
$this->chain[] = array(
'type'=>'range',
'not'=>true,
"allowed"=>$allowed
);
return $this;
}
public function group($name, $assertion = null, $chain = null){
$chain = $chain == null?($assertion == null ? $name : $assertion):$chain;
$assertion = is_string($assertion) ? $assertion : false;
$name = is_string($name) ? $name : false;
$this->chain[] = array(
'type'=>'group',
'assertion'=>$assertion,
'name'=>$name,
'chain'=>$chain->chain
);
return $this;
}
public function any(){
$this->chain[] = array("type"=>'any');
return $this;
}
public function repeat($min, $max = false){
$code = ($min == '?' && $min == '*' && $min == '+')?$min:'{'.($min?$min:0) . ($max===false?'':',' . ($max?$max:'')) . '}';
$this->chain[] = array(
"type"=>"repeat",
"code"=>$code
);
return $this;
}
public function raw($code){
$this->chain[] = array(
"type"=>'raw',
"code"=>$code
);
return $this;
}
public function text($text,$escape){
if($escape == null) $escape = true;
$this->chain[] = array(
'type'=>'text',
"code"=>$escape?RegCodeFn::escape($text):$text
);
return $this;
}
public function __toString(){
return "/" . RegCodeFn::compileArr($this->chain) . "/" . $this->flags;
}
public function compile(){
return new RegCodeCompiled(RegCodeFn::compileArr($this->chain), $this->flags);
}
}
class RegCodeCompiled {
public $code;
public $flags;
public function __construct($code, $flags){
$this->code = $code;
$this->flags = $flags;
}
public function match($subject, &$matches = null, $flags = 0, $offset = 0){
return preg_match("/" . $this->code . "/" . $this->flags, $subject, $matches, $flags, $offset);
}
public function match_all(){
}
public function replace($replacement, $subject, $limit = -1, &$count = null){
return preg_replace("/" . $this->code . "/" . $this->flags, $replacement, $subject, $limit, $count);
}
public function replace_callback($callback, $subject, $limit = -1, &$count = null){
return preg_replace_callback("/" . $this->code . "/" . $this->flags, $callback, $subject, $limit, $count);
}
}
$match;
RC()
->start()
->range('a-z')
->range('a-z0-9')->repeat(3,null)
->end()->compile()->match("all3tho",$match);
var_dump($match)
Enable javascript to submit You have javascript disabled. You will not be able to edit any code.
Here you find the average performance (time & memory) of each version. A grayed out version indicates it didn't complete successfully (based on exit-code).
Version System time (s) User time (s) Memory (MiB) 5.4.26 0.010 0.077 19.00 5.4.25 0.010 0.047 18.84 5.4.24 0.007 0.080 18.74 5.4.23 0.007 0.067 19.01 5.4.22 0.007 0.057 18.94 5.4.21 0.003 0.057 19.01 5.4.20 0.030 0.043 18.94 5.4.19 0.013 0.053 18.76 5.4.18 0.003 0.057 18.80 5.4.17 0.013 0.050 18.95 5.4.16 0.010 0.050 18.75 5.4.15 0.000 0.063 18.93 5.4.14 0.003 0.053 16.49 5.4.13 0.007 0.050 16.64 5.4.12 0.000 0.077 16.59 5.4.11 0.007 0.050 16.56 5.4.10 0.003 0.057 16.54 5.4.9 0.000 0.057 16.69 5.4.8 0.000 0.053 16.36 5.4.7 0.013 0.067 16.56 5.4.6 0.013 0.063 16.41 5.4.5 0.003 0.067 16.52 5.4.4 0.010 0.063 16.55 5.4.3 0.010 0.043 16.49 5.4.2 0.013 0.040 16.59 5.4.1 0.007 0.050 16.64 5.4.0 0.003 0.050 15.85 5.3.28 0.010 0.047 14.75 5.3.27 0.010 0.053 14.70 5.3.26 0.017 0.043 14.86 5.3.25 0.003 0.057 14.78 5.3.24 0.010 0.053 14.77 5.3.23 0.007 0.053 14.66 5.3.22 0.003 0.070 14.62 5.3.21 0.010 0.047 14.71 5.3.20 0.007 0.047 14.72 5.3.19 0.007 0.050 14.62 5.3.18 0.007 0.057 14.63 5.3.17 0.020 0.043 14.82 5.3.16 0.010 0.047 14.73 5.3.15 0.020 0.037 14.66 5.3.14 0.013 0.043 14.46 5.3.13 0.010 0.050 14.79 5.3.12 0.007 0.050 14.71 5.3.11 0.000 0.057 14.80 5.3.10 0.013 0.060 14.28 5.3.9 0.007 0.047 14.32 5.3.8 0.003 0.053 14.33 5.3.7 0.007 0.050 14.07 5.3.6 0.010 0.050 13.91 5.3.5 0.003 0.053 14.20 5.3.4 0.007 0.050 14.10 5.3.3 0.003 0.050 14.00 5.3.2 0.003 0.053 13.84 5.3.1 0.007 0.070 13.80 5.3.0 0.007 0.070 13.78 5.2.17 0.000 0.047 11.30 5.2.16 0.007 0.037 11.30 5.2.15 0.013 0.057 11.21 5.2.14 0.003 0.043 11.22 5.2.13 0.007 0.037 11.29 5.2.12 0.010 0.033 11.14 5.2.11 0.003 0.050 11.24 5.2.10 0.010 0.033 11.15 5.2.9 0.003 0.043 11.16 5.2.8 0.007 0.037 11.25 5.2.7 0.007 0.047 11.25 5.2.6 0.003 0.043 11.09 5.2.5 0.003 0.040 10.93 5.2.4 0.007 0.037 11.05 5.2.3 0.003 0.040 11.28 5.2.2 0.007 0.047 11.14 5.2.1 0.010 0.050 11.11 5.2.0 0.013 0.050 10.79 5.1.6 0.010 0.037 10.12 5.1.5 0.007 0.043 10.09 5.1.4 0.013 0.040 10.01 5.1.3 0.000 0.040 10.41 5.1.2 0.000 0.040 10.44 5.1.1 0.000 0.043 10.36 5.1.0 0.023 0.023 10.08 5.0.5 0.007 0.027 8.85 5.0.4 0.000 0.030 8.49 5.0.3 0.003 0.060 8.38 5.0.2 0.007 0.033 8.40 5.0.1 0.003 0.027 8.48 5.0.0 0.000 0.043 8.46 4.4.9 0.007 0.017 6.79 4.4.8 0.007 0.017 6.79 4.4.7 0.003 0.020 6.79 4.4.6 0.007 0.017 6.79 4.4.5 0.000 0.030 6.79 4.4.4 0.000 0.037 6.79 4.4.3 0.003 0.027 6.79 4.4.2 0.000 0.023 6.79 4.4.1 0.003 0.020 6.79 4.4.0 0.003 0.033 6.79 4.3.11 0.010 0.023 6.79 4.3.10 0.007 0.017 6.79 4.3.9 0.003 0.020 6.79 4.3.8 0.000 0.033 6.78 4.3.7 0.007 0.013 6.78 4.3.6 0.007 0.017 6.78 4.3.5 0.003 0.027 6.78 4.3.4 0.000 0.033 6.78 4.3.3 0.003 0.027 6.78 4.3.2 0.003 0.020 6.78 4.3.1 0.007 0.017 6.78 4.3.0 0.010 0.030 9.24
preferences:dark mode live preview
140.46 ms | 1394 KiB | 7 Q