Extracting matches from php regex

前端 未结 5 2080
深忆病人
深忆病人 2020-12-03 14:33

In perl regex we can extract the matched variables, ex below.

   # extract hours, minutes, seconds
   $time =~ /(\\d\\d):(\\d\\d):(\\d\\d)/; # match hh:mm         


        
5条回答
  •  眼角桃花
    2020-12-03 14:44

    You can just modify your current regexp to capture everything after the colon up to the first space:

    $subject = "E:contact@customer.com I:100955";
    $pattern = "/^E:([^ ]+)/";
    if (preg_match($pattern, $subject, $m)) {
        echo "Yes, A Match";
    }
    $email = $m[1];
    

    In case you're not familiar with regexp, [^ ]+ means "any character but a space" and it doesn't require a space to be present to work. If for any reason the input changes to "E:email@host.tld" without the " I:12345" bit, it will still work.

提交回复
热议问题