PHP class to generate regular expressions

Regular expressions, it is quite hard to write, so the package classes, the first draft, start a discussion.

On the regular, refer to the proposal  https://github.com/CyC2018/CS-Notes/blob/master/notes/%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE% E5% BC% 8F.md

 

class Pattern {

    public $pattern = '';

    public static function init(){

        return new static();
    }

    public function start($char){

        $this->pattern .= '^' . $char;
        return $this;
    }


    public function end($char){

        $this->pattern .=  $char . '$';
        return $this;
    }

    // the number of matches 
    public  function  COUNT ( $ Range = null ) {

        if(is_array($range)) $range = implode(',', $range);

        if(is_numeric($range) || strpos($range, ',') ) $range =  '{' . $range . '}';
        $this->pattern .=  $range;

        return $this;
    }



    public function match($pattern, $get = true){

        $this->pattern .= '(' . ($get ? '' : '?:'). $pattern . ')';
        return $this;
    }

    // escape 
    public  function Escape ( $ char ) {

        $this->pattern .=  '\\' . $char;
        return $this;
    }

    public function append($string, $comment = null){

        $this->pattern .= $string;
        //$this->pattern .= $string . ($comment ? '(?#'. $comment .')' : '');
        return $this;
    }


    public function flush(){

        $pattern = $this->pattern;
        $this->pattern = '';

        return $pattern;
    }

    public static function help(){

        Help $ = <<< 'Help' 
        [\ B] => a word boundary
        [\ B] => non-word boundary
        [\ CX] => x specified control character, AZ value must be a- or one z
        [\ D] => number, [0-9 ]
        [\ D] => a non-numeric, [^ 0-9 ].
        [\ F] => page breaks, equivalent to \ X0C and \ cL
        [\ n-] => identifies an octal escape value or a backward reference. If before \ n at least n captured subexpressions, n is a reference back. Otherwise, if n is an octal digit (0-7 ), then n is an octal escape value.
        [\ R & lt] => carriage return, equivalent to \ x0d and \ cM
        [\ S] => a blank character, equivalent to [\ f \ n \ r \ t \ v]
        [\ S] => non-blank character, equivalent to [^ \ F \ n-\ R & lt \ T \ V]
        [\ T] => horizontal tab, equivalent to \ x09 and \ the cI
        [\ V] => vertical tabs. Equivalent to \ x0b and \ cK
        [\ W] => letters, numbers, underscores, equivalent to [A-Za-z0- 9_]
        [\ W is] => non-alphabetic, non-numeric, non-underlined. It is equivalent to [A-Za-z0- ^ 9_]
        [\ Xn] => character matches a hexadecimal representation, the value must be determined by two numbers long. \ x41B expressed AB
        [\ NUM] => a reference to the matching of the acquired "(.) \ 1" matches two consecutive identical characters
        [\ nm] => identifies an octal escape value or a backward reference. If you have to obtain a sub-expressions nm at least before nm \, then nm is a reference back. If \ There are at least n before obtaining nm, then n is a backward reference character m in the heel. When these conditions are not met, if n and m are octal digits (0-7 ), the \ nm matches octal escape value nm
        [\ NML] => if n is an octal digit (0-3), and m and l are octal digits (0-7 ), the matching octal escape value nml.
        [\ un] => the n-four hexadecimal representation of the Unicode character number
help;
        echo $help;

    }


    // Forward Lookup 
    public  function Forward ( $ String , $ positive = to true ) {

        $this->pattern .=  '(?' . ($positive ? '=' : '!') . $string . ')';

        return $this;

    }


    // reverse lookup 
    public  function Backward ( $ String , $ positive = to true ) {

        $this->pattern .=  '(?<' . ($positive ? '=' : '!') . $string . ')';

        return $this;

    }

    public function exec($mode = null){

        $mode = $mode ? $mode : '';
        $pattern = '/' . $this->pattern . '/' . $mode;

        return $pattern;
    }


    // criteria to find 
    public  function for condition Condition ( $ for condition Condition , $ String ) {

        $this->pattern .= '(?(' . $condition. ')'. $string .')';

        return $this;
    }




}

$html = "<b>example: </b><div align=left>this is a test</div>";

$ pattern = Pattern :: the init () -> the append ( '? <(\ w +)', 'label name' )
             -> the append (, 'space' '\ S *?' )
             -> the append ( '*.? > ',' property ' )
             .? -> the append (' * ',' content ' )
             -> the append (' <\ / \ 1> ',' trackback label name ' )
             -> Exec ();

echo $pattern . "\n";
preg_match_all($pattern, $html, $out);
print_r($out);

 

Guess you like

Origin www.cnblogs.com/zbseoag/p/11100662.html