4 public function __construct($r, $opt) {
5 if( !php_Boot::$skip_constructor ) {
7 $a = _hx_explode("g", $opt);
8 $this->hglobal = $a->length > 1;
12 $this->options = $opt;
13 $this->re = "/" . ((str_replace("/", "\\/", $r) . "/") . $opt);
22 public function match($s) {
23 $p = preg_match($this->re, $s, $this->matches, PREG_OFFSET_CAPTURE);
32 public function matched($n) {
34 throw new HException("EReg::matched");
36 if($n >= count($this->matches)) {
39 if($this->matches[$n][1] < 0) {
42 return $this->matches[$n][0];
44 public function matchedLeft() {
45 if(count($this->matches) === 0) {
46 throw new HException("No string matched");
48 return _hx_substr($this->last, 0, $this->matches[0][1]);
50 public function matchedRight() {
51 if(count($this->matches) === 0) {
52 throw new HException("No string matched");
54 $x = $this->matches[0][1] + strlen($this->matches[0][0]);
55 return _hx_substr($this->last, $x, null);
57 public function matchedPos() {
58 return _hx_anonymous(array("pos" => $this->matches[0][1], "len" => strlen($this->matches[0][0])));
60 public function split($s) {
61 return new _hx_array(preg_split($this->re, $s, $this->hglobal ? -1 : 2));
63 public function replace($s, $by) {
64 $by = str_replace("\$\$", "\\\$", $by);
65 if(!preg_match('/\\([^?].+?\\)/', $this->re)) $by = preg_replace('/\$(\d+)/', '\\\$\1', $by);
66 return preg_replace($this->re, $by, $s, ($this->hglobal ? -1 : 1));
68 public function customReplace($s, $f) {
69 $buf = new StringBuf();
71 if(!$this->match($s)) {
74 $buf->b .= $this->matchedLeft();
75 $buf->b .= call_user_func_array($f, array($this));
76 $s = $this->matchedRight();
82 public function __call($m, $a) {
83 if(isset($this->$m) && is_callable($this->$m))
84 return call_user_func_array($this->$m, $a);
85 else if(isset($this->»dynamics[$m]) && is_callable($this->»dynamics[$m]))
86 return call_user_func_array($this->»dynamics[$m], $a);
87 else if('toString' == $m)
88 return $this->__toString();
90 throw new HException('Unable to call «'.$m.'»');
92 function __toString() { return 'EReg'; }