@ 2014-03-25T09:24:02Z <?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['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?:"";
}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?:0) . ($max===false?',' . ($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($force){
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);
}
}
echo RC()
->start()
->range('a-z')
->range('a-z0-9')->repeat(3,null)
->end();
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.090 0.047 19.16 5.4.25 0.070 0.040 19.15 5.4.24 0.093 0.030 18.93 5.4.23 0.073 0.030 18.89 5.4.22 0.067 0.050 18.98 5.4.21 0.087 0.040 18.98 5.4.20 0.043 0.033 19.04 5.4.19 0.053 0.070 18.92 5.4.18 0.057 0.030 19.00 5.4.17 0.067 0.040 18.95 5.4.16 0.037 0.050 18.93 5.4.15 0.077 0.080 19.03 5.4.14 0.070 0.037 16.46 5.4.13 0.057 0.033 16.54 5.4.12 0.047 0.033 16.35 5.4.11 0.067 0.030 16.48 5.4.10 0.077 0.027 16.48 5.4.9 0.057 0.030 16.48 5.4.8 0.063 0.047 16.30 5.4.7 0.030 0.047 16.56 5.4.6 0.080 0.033 16.41 5.4.5 0.057 0.033 16.48 5.4.4 0.080 0.033 16.37 5.4.3 0.057 0.050 16.45 5.4.2 0.063 0.053 16.21 5.4.1 0.070 0.030 16.54 5.4.0 0.067 0.063 15.99 5.3.29 0.063 0.043 14.65 5.3.28 0.083 0.030 14.68 5.3.27 0.060 0.030 14.69 5.3.26 0.050 0.037 14.59 5.3.25 0.033 0.037 14.65 5.3.24 0.053 0.033 14.69 5.3.23 0.030 0.037 14.65 5.3.22 0.070 0.030 14.63 5.3.21 0.043 0.043 14.71 5.3.20 0.050 0.040 14.72 5.3.19 0.060 0.027 14.55 5.3.18 0.047 0.023 14.64 5.3.17 0.020 0.040 14.65 5.3.16 0.053 0.060 14.79 5.3.15 0.063 0.067 14.63 5.3.14 0.053 0.037 14.60 5.3.13 0.080 0.057 14.54 5.3.12 0.040 0.057 14.64 5.3.11 0.063 0.063 14.61 5.3.10 0.090 0.043 14.26 5.3.9 0.070 0.043 14.21 5.3.8 0.093 0.037 14.11 5.3.7 0.090 0.033 14.10 5.3.6 0.043 0.063 14.00 5.3.5 0.040 0.060 13.95 5.3.4 0.090 0.040 13.95 5.3.3 0.080 0.033 13.96 5.3.2 0.023 0.037 13.79 5.3.1 0.090 0.037 13.80 5.3.0 0.083 0.033 13.79 5.2.17 0.073 0.027 11.09 5.2.16 0.000 0.030 11.31 5.2.15 0.060 0.030 11.17 5.2.14 0.070 0.040 11.11 5.2.13 0.057 0.037 11.04 5.2.12 0.067 0.033 11.20 5.2.11 0.057 0.033 11.02 5.2.10 0.063 0.027 11.05 5.2.9 0.060 0.043 11.13 5.2.8 0.070 0.030 11.04 5.2.7 0.073 0.030 11.16 5.2.6 0.073 0.027 11.07 5.2.5 0.047 0.050 10.94 5.2.4 0.063 0.033 11.01 5.2.3 0.040 0.033 10.91 5.2.2 0.047 0.033 10.93 5.2.1 0.070 0.037 10.89 5.2.0 0.067 0.030 10.79 5.1.6 0.063 0.020 10.00 5.1.5 0.070 0.023 10.02 5.1.4 0.067 0.023 10.05 5.1.3 0.067 0.027 10.29 5.1.2 0.007 0.040 10.45 5.1.1 0.067 0.027 10.24 5.1.0 0.063 0.023 10.14 5.0.5 0.033 0.027 8.47 5.0.4 0.023 0.020 8.33 5.0.3 0.030 0.053 8.23 5.0.2 0.030 0.020 8.47 5.0.1 0.040 0.020 8.13 5.0.0 0.047 0.050 8.10 4.4.9 0.030 0.013 7.46 4.4.8 0.000 0.017 7.44 4.4.7 0.033 0.033 7.43 4.4.6 0.030 0.033 7.43 4.4.5 0.030 0.027 7.43 4.4.4 0.007 0.047 7.13 4.4.3 0.047 0.023 7.12 4.4.2 0.023 0.013 7.11 4.4.1 0.030 0.020 6.89 4.4.0 0.000 0.030 6.89 4.3.11 0.033 0.027 6.88 4.3.10 0.037 0.020 6.88 4.3.9 0.057 0.030 6.88 4.3.8 0.033 0.020 6.87 4.3.7 0.040 0.017 6.87 4.3.6 0.043 0.017 6.87 4.3.5 0.000 0.017 6.86 4.3.4 0.023 0.043 6.73 4.3.3 0.007 0.027 6.59 4.3.2 0.027 0.030 6.52 4.3.1 0.013 0.013 6.52 4.3.0 0.023 0.023 6.36
preferences:dark mode live preview
135.63 ms | 1394 KiB | 7 Q