对字符串进行正则表达式归纳


function getNames($names){

    foreach ($names as $key => $name) {
        $ysname = $name;

        $name = str_replace(')',')', $name);
        $name = str_replace('(','(', $name);    
        $name = str_replace(' ',' ', $name);
        $name = trim( $name );
        $name = trim( $name , ',' );

        if($name == '') continue;

        $str  = $name;
        
        $tempReg = [];

        for($i=0;$i<strlen($name);$i++){
            $lastReg = end($tempReg);
            $lastRegIndex = count($tempReg) - 1;

            if(preg_match('/^[\x{4e00}-\x{9fa5}].*?/u',$str)){
                $str = substr($str, 3);
                
                if($lastReg == '([\x{4e00}-\x{9fa5}])'){
                    $tempReg[ $lastRegIndex ] = '([\x{4e00}-\x{9fa5}]{2})';
                }else if( preg_match('/9fa5\}\]\{(\d+)\}\)$/', $lastReg , $match ) ){
                    $tempReg[ $lastRegIndex ] = '([\x{4e00}-\x{9fa5}]{'. ( $match[1] + 1 ) .'})';
                }else{
                    $tempReg[] = '([\x{4e00}-\x{9fa5}])';
                }

                $i = $i + 2;
            }else if(preg_match('/^、.*?/u',$str)){
                $str = substr($str, 3);
                $tempReg[] = '(、)';
                $i = $i + 2;
            }else if(preg_match('/^(.*?/u',$str)){
                $str = substr($str, 3);
                $tempReg[] = '(()';
                $i = $i + 2;
            }else if(preg_match('/^).*?/u',$str)){
                $str = substr($str, 3);
                $tempReg[] = '())';
                $i = $i + 2;
            }else if(preg_match('/^ .*?/u',$str)){
                $str = substr($str, 3);
                $tempReg[] = '( )';
                $i = $i + 2;
            }else if(preg_match('/^:.*?/u',$str)){
                $str = substr($str, 3);
                $tempReg[] = '(:)';
                $i = $i + 2;
            }else if(preg_match('/^,.*?/u',$str)){
                $str = substr($str, 3);
                $tempReg[] = '(,)';
                $i = $i + 2;
            }else if(preg_match('/^;.*?/u',$str)){
                $str = substr($str, 3);
                $tempReg[] = '(;)';
                $i = $i + 2;
            }else if(preg_match('/^【.*?/u',$str)){
                $str = substr($str, 3);
                $tempReg[] = '(【)';
                $i = $i + 2;
            }else if(preg_match('/^】.*?/u',$str)){
                $str = substr($str, 3);
                $tempReg[] = '(】)';
                $i = $i + 2;
            }else if(preg_match('/^。.*?/u',$str)){
                $str = substr($str, 3);
                $tempReg[] = '(。)';
                $i = $i + 2;
            }else if(preg_match('/^#.*?/u',$str)){
                $str = substr($str, 3);
                $tempReg[] = '(#)';
                $i = $i + 2;
            }else if(preg_match('/^[a-zA-Z].*?/',$str)){
                $str = substr($str, 1);

                if($lastReg == '([a-zA-Z])'){
                    $tempReg[ $lastRegIndex ] = '([a-zA-Z]{2})';
                }else if( preg_match('/^\(\[a-zA-Z\]\{(\d+)\}\)$/', $lastReg , $match ) ){
                    $tempReg[ $lastRegIndex ] = '([a-zA-Z]{'. ( $match[1] + 1 ) .'})';
                }else{
                    $tempReg[] = '([a-zA-Z])';
                }

            }else if(preg_match('/^\d.*?/',$str)){
                $str = substr($str, 1);
 
                if($lastReg == '(\d)'){
                    $tempReg[ $lastRegIndex ] = '(\d{2})';
                }else if( preg_match('/\(\\\d\{(\d+)\}\)$/', $lastReg , $match ) ){
                    $tempReg[ $lastRegIndex ] = '(\d{'. ( $match[1] + 1 ) .'})';
                }else{
                    $tempReg[] = '(\d)';
                }

            }else if(preg_match('/^_.*?/',$str)){
                $str = substr($str, 1);
                $tempReg[] = '(_)';
            }else if(preg_match('/^-.*?/',$str)){
                $str = substr($str, 1);
                $tempReg[] = '(-)';
            }else if(preg_match('/^\(.*?/',$str)){
                $str = substr($str, 1);
                $tempReg[] = '(\()';
            }else if(preg_match('/^\).*?/',$str)){
                $str = substr($str, 1);
                $tempReg[] = '(\))';
            }else if(preg_match('/^\..*?/',$str)){
                $str = substr($str, 1);
                $tempReg[] = '(\.)';
            }else if(preg_match('/^\[.*?/',$str)){
                $str = substr($str, 1);
                $tempReg[] = '(\[)';
            }else if(preg_match('/^\].*?/',$str)){
                $str = substr($str, 1);
                $tempReg[] = '(\])';
            }else if(preg_match('/^\/.*?/',$str)){
                $str = substr($str, 1);
                $tempReg[] = '(\/)';
            }else if(preg_match('/^,.*?/',$str)){
                $str = substr($str, 1);
                $tempReg[] = '(,)';
            }else if(preg_match('/^:.*?/',$str)){
                $str = substr($str, 1);
                $tempReg[] = '(:)';
            }else if(preg_match('/^;.*?/',$str)){
                $str = substr($str, 1);
                $tempReg[] = '(;)';
            }else if(preg_match('/^ .*?/',$str)){
                $str = substr($str, 1);
                $tempReg[] = '( )';
            }else if(preg_match('/^#*?/',$str)){
                $str = substr($str, 1);
                $tempReg[] = '(#)';
            }else if(preg_match('/^·.*?/',$str)){
                $str = substr($str, 2);
                $tempReg[] = '(·)';
                $i = $i + 1;
            }else{
                var_dump($ysname);
                echo $str . " weizhi\n";
                var_dump($str);
                $tempReg[] = '(未知)';
            }
        }

        $tempReg = implode('', $tempReg);
        $allRegData[$tempReg][] = $ysname;
    }

    return $allRegData;
}

上述代码的功能主要是对数量较大、单条字符长度不长的文字进行正则表达式分类归纳。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值