2015-05-28 71 views
-2

蔭非常新的PHP正則表達式,但不知何故學會了一下,如下嘗試過,但得到的錯誤是:PHP Warning: preg_match(): Unknown modifier '[' in /home/3ZZyLt/prog.php on line 4PHP的正則表達式錯誤的preg_match():未知的修飾詞「[」

這裏是整個代碼的輸出:從上面的鏈接https://ideone.com/fTIyUK

同一代碼:

<?php 
$email = "paulw Paul Walker paulw 2014-12-28 07:18:09 [email protected] 2014-12-28 07:18:09 2014-12-28 07:18:09"; // Invalid email address 
$regex = "/[a-z] [A_Z] [A-Z] [A-Z] (\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2}) /[-0-9a-zA-Z.+_][email protected][-0-9a-zA-Z.+_]+\.[a-zA-Z]{2,4} (\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2}) (\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})/"; 
if (preg_match($regex, $email)) { 
    echo $email . " is a valid email. We can accept it."; 
} else { 
    echo $email . " is an invalid email. Please try again."; 
} 
?> 

請告訴我有什麼需要改變?

更新:

輸入:串(AZ)的字符串(A-ZA-Z)的字符串(A-ZA-Z)的字符串(AZ)日期(YYYY-MM-DD HH:MM :SS)EMAILID日期(YYYY-MM-DD HH:MM:SS)日期(YYYY-MM-DD HH:MM:SS)

8參數:

userid (string) 
first name(string) 
last name(string) 
userid(string) 
date 
email 
date 
date 
+2

這是可怕的正則表達式開始,並不會如預期反正工作。爲什麼不先在空間上分割?這是一個空格分隔的日誌/任何行,不是嗎? – mario

+1

前四組只捕獲一個單個字符。即使錯誤消失,這也不起作用。 – Luceos

+3

[如果你想驗證一封電子郵件,你可以使用filter_var()](http://stackoverflow.com/questions/19522092/should-i-use-filter-var-to-validate-email) – castis

回答

1
$regex = "/[a-z] [A_Z] [A-Z] [A-Z] (\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2}) /[-0-9a-zA-Z.+_][email protected][-0-9a-zA-Z.+_]+\.[a-zA-Z]{2,4} (\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2}) (\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})/"; 

如果您仔細看到此正則表達式,則在正則表達式中間有一個未轉義的/,但您使用/作爲正則表達式分隔符。

使用這個表達式,而不是一個替代正則表達式分隔符:

$regex = "~[a-z]+ [A-Z]+ [A-Z]+ [A-Z]+ (\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2}) [-\w.+][email protected][-\w.+]+\.[a-zA-Z]{2,4} (\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2}) (\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})~i"; 

RegEx Demo

+0

嗨,我試過你的正則表達式,現在得到輸出爲'無效',在這裏:https://ideone.com/fTIyUK – user3815806

+1

我沒有改變你的正則表達式,它是一樣的。我只是改變了正則表達式分隔符。將檢查你的ideone代碼 – anubhava

+0

你能修改我的正則表達式代碼,並使其工作? – user3815806

相關問題