@ 2014-03-25T09:23:45Z <?php
function RC($flags = ""){
return new RegCode($flags);
}
static 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.030 0.073 19.13 5.4.25 0.027 0.033 19.19 5.4.24 0.010 0.043 19.07 5.4.23 0.020 0.070 18.83 5.4.22 0.010 0.047 19.13 5.4.21 0.013 0.040 19.08 5.4.20 0.017 0.040 19.08 5.4.19 0.017 0.043 18.86 5.4.18 0.023 0.073 19.01 5.4.17 0.017 0.043 19.18 5.4.16 0.013 0.043 18.99 5.4.15 0.027 0.043 18.82 5.4.14 0.023 0.067 16.26 5.4.13 0.020 0.040 16.40 5.4.12 0.027 0.030 16.34 5.4.11 0.017 0.067 16.56 5.4.10 0.020 0.037 16.46 5.4.9 0.013 0.073 16.54 5.4.8 0.020 0.070 16.26 5.4.7 0.020 0.057 16.46 5.4.6 0.013 0.057 16.52 5.4.5 0.013 0.043 16.48 5.4.4 0.017 0.037 16.53 5.4.3 0.027 0.057 16.25 5.4.2 0.017 0.040 16.51 5.4.1 0.020 0.057 16.32 5.4.0 0.033 0.060 15.90 5.3.28 0.027 0.040 14.52 5.3.27 0.030 0.043 14.61 5.3.26 0.027 0.073 14.71 5.3.25 0.030 0.067 14.72 5.3.24 0.030 0.067 14.68 5.3.23 0.020 0.070 14.64 5.3.22 0.037 0.050 14.57 5.3.21 0.020 0.073 14.68 5.3.20 0.023 0.063 14.54 5.3.19 0.030 0.067 14.47 5.3.18 0.033 0.063 14.55 5.3.17 0.023 0.073 14.57 5.3.16 0.027 0.067 14.58 5.3.15 0.020 0.057 14.49 5.3.14 0.023 0.070 14.47 5.3.13 0.027 0.050 14.66 5.3.12 0.027 0.043 14.62 5.3.11 0.020 0.047 14.47 5.3.10 0.020 0.077 14.12 5.3.9 0.017 0.043 13.99 5.3.8 0.020 0.037 14.10 5.3.7 0.027 0.060 14.13 5.3.6 0.013 0.040 13.96 5.3.5 0.020 0.070 13.86 5.3.4 0.023 0.047 13.94 5.3.3 0.023 0.060 13.82 5.3.2 0.033 0.033 13.61 5.3.1 0.027 0.050 13.71 5.3.0 0.023 0.057 13.69 5.2.17 0.020 0.037 13.00 5.2.16 0.010 0.033 13.00 5.2.15 0.013 0.033 13.00 5.2.14 0.013 0.030 13.00 5.2.13 0.017 0.027 13.00 5.2.12 0.017 0.023 13.00 5.2.11 0.017 0.030 13.00 5.2.10 0.013 0.027 13.00 5.2.9 0.017 0.023 13.00 5.2.8 0.010 0.030 13.00 5.2.7 0.013 0.027 13.00 5.2.6 0.017 0.023 13.00 5.2.5 0.007 0.040 13.00 5.2.4 0.013 0.027 13.00 5.2.3 0.007 0.030 13.00 5.2.2 0.017 0.023 13.00 5.2.1 0.010 0.027 13.00 5.2.0 0.010 0.037 13.00 5.1.6 0.010 0.030 13.00 5.1.5 0.020 0.023 13.00 5.1.4 0.017 0.017 13.00 5.1.3 0.003 0.030 13.00 5.1.2 0.013 0.023 13.00 5.1.1 0.013 0.023 13.00 5.1.0 0.010 0.027 13.00 5.0.5 0.003 0.023 13.00 5.0.4 0.003 0.023 13.00 5.0.3 0.007 0.030 13.00 5.0.2 0.007 0.020 13.00 5.0.1 0.010 0.013 13.00 5.0.0 0.007 0.033 13.00 4.4.9 0.007 0.010 13.00 4.4.8 0.000 0.017 13.00 4.4.7 0.007 0.013 13.00 4.4.6 0.003 0.017 13.00 4.4.5 0.003 0.017 13.00 4.4.4 0.010 0.020 13.00 4.4.3 0.007 0.013 13.00 4.4.2 0.003 0.017 13.00 4.4.1 0.003 0.020 13.00 4.4.0 0.003 0.027 13.00 4.3.11 0.000 0.020 13.00 4.3.10 0.003 0.020 13.00 4.3.9 0.003 0.017 13.00 4.3.8 0.000 0.027 13.00 4.3.7 0.007 0.017 13.00 4.3.6 0.010 0.013 13.00 4.3.5 0.007 0.020 13.00 4.3.4 0.007 0.023 13.00 4.3.3 0.000 0.013 13.00 4.3.2 0.000 0.017 13.00 4.3.1 0.000 0.017 13.00 4.3.0 0.000 0.020 13.00
preferences:dark mode live preview
150.71 ms | 1394 KiB | 7 Q