2013-04-25 190 views
0

我的正則表達式是:正則表達式在PHP不工作

$regex = '/(?<=Α:)(([\w-\.]+)@((?:[\w]+\.)+)([a-zA-Z]{2,4}))/';

我等內容是:

Q: Email Address 
A: [email protected] 

拉德軟件正則表達式設計師說,它應該工作。

各種在線網站都會返回正確的結果。

如果我刪除(?< =Α:)向後看,正則表達式會正確返回所有電子郵件。

當我從php運行它時,它不返回任何匹配。

發生了什麼事?

我還使用了特定類型的正則表達式(即(< =電子郵件:?不同的內容),它工作得很好,在這種情況下

+1

你使用哪些函數來解析正則表達式? preg_ *,eregi_ *? – phpisuber01 2013-04-25 19:45:46

+0

@ phpisuber01 preg_match()。 – jimmy 2013-04-25 19:52:17

+0

正則表達式中的「A」有一些變音符號,內容中的「A」是普通字母。 – Barmar 2013-04-25 20:00:54

回答

0

的問題是,你的正則表達式中包含Α,其中有超過它的口音,但內容包含A,這沒有。所以lookbehind不匹配。

我改變正則表達式:

$regex = '/(?<=A:)(([\w-\.]+)@((?:[\w]+\.)+)([a-zA-Z]{2,4}))/'; 

和它的作品。

+0

你一定是在跟我開玩笑...... 花了一個小時,試圖找出什麼是錯的,最後我想找一個希臘語'A',當我想要一個英語的時候...... – jimmy 2013-04-25 20:20:56

0

您正則表達式的問題本身之外,你真的應該考慮的問題。不要試圖編寫自己的電子郵件地址正則表達式解析器。請參閱stackoverflow post:Using a regular expression to validate an email address爲什麼 - upshot:RFC是長期的,並且要求您的正則表達式能力。

+0

是的,我已經看到正確提取RFC電子郵件地址所需的正則表達式的怪物! – jimmy 2013-04-25 19:50:57

1

這是我的較新的怪物腳本,用於驗證e郵件「驗證」或沒有,你可以餵它奇怪的東西,並打破它,但在生產過程中,它處理了我遇到的99.99999999%的問題。操作系統。

<?php 

$pattern = '!^[^@\s][email protected][^[email protected]\s]+\.[^@\s]+$!'; 

$examples = array(
    '[email protected]', 
    '[email protected]', 
    '[email protected]', 
    '[email protected]', 
    '[email protected]', 
    '@google.com', 
    '[email protected]@my.com', 
    'my [email protected]', 
); 


foreach($examples as $test_mail){ 
    if(preg_match($pattern,$test_mail)){ 
     echo ("$test_mail - passes\n"); 
    } else { 
     echo ("$test_mail - fails\n");     
    } 
} 

?> 

輸出

  1. [email protected] - 傳遞
  2. [email protected] - 傳遞
  3. [email protected] - 通過
  4. bad.email @ .. email.com - 失敗
  5. [email protected] - 失敗
  6. @ google.com - 失敗
  7. 我@電子郵件@ my.com - 失敗
  8. [email protected] - 失敗

除非有針對的理由向後看,你可以匹配preg_match_all()中的所有電子郵件。既然你有一個字符串的工作,你會稍微修改小幅正則表達式:

$string_only_pattern = '!\s([^@\s][email protected][^[email protected]\s]+\.[^@\s]+)\s!s'; 

$mystring = ' 
[email protected] - passes 
[email protected] - passes 
[email protected] - passes 
[email protected] - fails 
[email protected] - fails 
@google.com - fails 
[email protected]@my.com - fails 
my [email protected] - fails 
'; 

preg_match_all($string_only_pattern,$mystring,$matches); 

print_r ($matches[1]); 

從字符串只

Array 
(
    [0] => [email protected] 
    [1] => [email protected] 
    [2] => [email protected] 
    [3] => [email protected] 
) 
+0

這與這個問題有什麼關係,這個問題是關於「A:」後視? – Barmar 2013-04-25 20:07:05

+0

@Barmar我正在接近它。對不起,我只寫了160wpm。 – 2013-04-25 20:22:31

1

輸出你是不是最有可能不使用DOTALL標誌s這裏這將讓DOT匹配換行符在你的正則表達式:

$str = <<< EOF 
Q: Email Address 
A: [email protected] 
EOF; 
if (preg_match_all('/(?<=A:)(([\w-\.]+)@((?:[\w]+\.)+)([a-zA-Z]{2,4}))/s', 
        $str, $arr)) 
    print_r($arr); 

OUTPUT:

Array 
(
    [0] => Array 
     (
      [0] => [email protected] 
     ) 

    [1] => Array 
     (
      [0] => [email protected] 
     ) 

    [2] => Array 
     (
      [0] => name 
     ) 

    [3] => Array 
     (
      [0] => example. 
     ) 

    [4] => Array 
     (
      [0] => com 
     ) 

) 
0

您的主題中的A字符是代碼爲65(unicode或ascii)的「普通」字符。但是,您在模式的後視中使用的A具有代碼913(unicode)。他們看起來很相似,但是不同。