@ 2014-03-25T09:22:59Z <?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.007 0.036 12.38 5.4.25 0.006 0.038 12.38 5.4.24 0.004 0.036 12.38 5.4.23 0.008 0.037 12.37 5.4.22 0.004 0.040 12.38 5.4.21 0.007 0.037 12.38 5.4.20 0.005 0.039 12.37 5.4.19 0.007 0.041 12.37 5.4.18 0.007 0.035 12.37 5.4.17 0.006 0.036 12.38 5.4.16 0.005 0.036 12.37 5.4.15 0.004 0.036 12.37 5.4.14 0.005 0.038 12.06 5.4.13 0.006 0.036 12.04 5.4.12 0.007 0.049 12.01 5.4.11 0.007 0.041 12.00 5.4.10 0.007 0.036 12.00 5.4.9 0.008 0.035 12.00 5.4.8 0.004 0.038 12.00 5.4.7 0.005 0.038 12.00 5.4.6 0.005 0.035 12.00 5.4.5 0.008 0.034 11.99 5.4.4 0.005 0.035 11.98 5.4.3 0.008 0.034 11.98 5.4.2 0.005 0.034 11.97 5.4.1 0.007 0.036 11.98 5.4.0 0.007 0.038 11.47 5.3.28 0.005 0.038 12.71 5.3.27 0.007 0.038 12.72 5.3.26 0.005 0.040 12.72 5.3.25 0.004 0.038 12.72 5.3.24 0.005 0.038 12.72 5.3.23 0.008 0.035 12.71 5.3.22 0.007 0.036 12.68 5.3.21 0.005 0.040 12.68 5.3.20 0.011 0.032 12.68 5.3.19 0.004 0.038 12.68 5.3.18 0.006 0.036 12.68 5.3.17 0.006 0.036 12.67 5.3.16 0.005 0.036 12.67 5.3.15 0.006 0.038 12.67 5.3.14 0.005 0.043 12.66 5.3.13 0.007 0.046 12.66 5.3.12 0.006 0.039 12.66 5.3.11 0.005 0.039 12.66 5.3.10 0.006 0.039 12.12 5.3.9 0.006 0.037 12.08 5.3.8 0.004 0.042 12.08 5.3.7 0.008 0.041 12.08 5.3.6 0.007 0.040 12.06 5.3.5 0.008 0.036 12.00 5.3.4 0.005 0.041 12.00 5.3.3 0.007 0.039 11.95 5.3.2 0.005 0.043 11.73 5.3.1 0.007 0.036 11.69 5.3.0 0.006 0.036 11.68
preferences:dark mode live preview
139.39 ms | 1394 KiB | 7 Q