正则表达式查找反斜杠,而不是特殊字符

时间:2015-08-07 04:04:37

标签: regex

如何在特殊字符中搜索反斜杠字符?例如,如果我有一个字符串' \ ba \ wa',我的正则表达式' \\。a'不会找到任何一个组,因为它会对待' \ b'和' \ w'作为单个特殊字符。

3 个答案:

答案 0 :(得分:0)

逃避反斜杠:

function send_email($process = "",$replace_fields=array(),$replace_with=array(),$email_template=null,$to=null,$extraTemplate = null)
    {

        // Loaded EmailTemplate Model
        $this->loadModel('EmailTemplate');
        $template=$this->EmailTemplate->find("first",array("conditions"=>array('EmailTemplate.alias'=>$email_template)));

        //  Used for Custom Templates
        if ($extraTemplate != '')
        {
            $template_data = $extraTemplate;
        }
        else
        {
            $template_data=$template['EmailTemplate']['description'];       
        }

        $replace_fields = array_merge($replace_fields, array('{logo}'));
        $logoSrc = HTTP_ROOT.'img/front/logo.png';

        $replace_with = array_merge($replace_with, array($logoSrc));
        $template_info=str_replace($replace_fields,$replace_with,$template_data);
        $template_info=str_replace(HTTP_ROOT,'/maheswarDev/',$template_info);
        $template_info=str_replace('/maheswarDev/',HTTP_ROOT,$template_info);

        //print_R($template_info); die;

        // SMTP Options
        $this->Email->smtpOptions = array(
            'port'=>"25", 
            'timeout'=>'30',
            'host' => 'mail.mindstack.in',
            'username'=>'my-email',
            'password'=>'my-password'
        );

         // Set delivery method
        $this->Email->delivery = 'smtp';
        $this->Email->SMTPAuth = true;
        $this->Email->SMTPSecure = 'tls';
        $this->Email->charset  = 'UTF-8';
        $this->set('data',$template_info);
        $this->Email->to = $to;
        $this->Email->subject = $template['EmailTemplate']['subject'];
        $this->Email->from = $template['EmailTemplate']['from'];
        $this->Email->template = 'email_template';
        $this->Email->sendAs = 'both'; 


        $this->Email->send();

        // Check for SMTP errors.
        $this->set('smtp_errors', $this->Email->smtpError);
    }

转义序列'\\.a' 是反斜杠字符。

答案 1 :(得分:0)

这个会起作用

 /\\.*a/

请参阅反驳Regex fiddle link

答案 2 :(得分:0)

公共课测试{

public static void main(String[] args) {
    String input="\\ba \\wa";
    System.out.println(input.matches("\\\\ba \\\\wa"));
}

}

产生真实。