user3815806 user3815806 - 6 months ago 30
PHP Question

php regex error preg_match(): Unknown modifier '['

Iam very new to Php Regex, but somehow learned bit and tried as below, but getting error as :

PHP Warning: preg_match(): Unknown modifier '[' in /home/3ZZyLt/prog.php on line 4


Here is the whole code with output:
https://ideone.com/fTIyUK


Same code from above link:

<?php
$email = "paulw Paul Walker paulw 2014-12-28 07:18:09 paul@comp.com 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.+_]+@[-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.";
}
?>


Please tell me what needs to be changed?

Updated:

Input: string(a-z) string(a-zA-Z) string(a-zA-Z) string(a-z) date(yyyy-mm-dd hh:mm:ss) emailid date(yyyy-mm-dd hh:mm:ss) date(yyyy-mm-dd hh:mm:ss)

8 Parameters:

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

Answer
$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.+_]+@[-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 you see this regex closely you have an unescaped / in the middle of regex but you're using / as regex delimiter.

Use this regex instead with an alternative regex delimiter:

$regex = "~[a-z]+ [A-Z]+ [A-Z]+ [A-Z]+ (\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2}) [-\w.+]+@[-\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

Comments