-2

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
user3815806
  • 243
  • 5
  • 15
  • 2
    That's horrible regex to begin with, and not going to work as expected anyway. Why not split on spaces first? It's a space-delimited log/whatever line, isn't it? – mario May 28 '15 at 16:13
  • 1
    The first four groups only capture one single character. This won't work even if the error goes away. – Luceos May 28 '15 at 16:19
  • 3
    [if you're trying to validate an email, you could use filter_var()](http://stackoverflow.com/questions/19522092/should-i-use-filter-var-to-validate-email) – castis May 28 '15 at 16:19
  • Is this whole regex to validate an email? Why not `.*?\s+(\S+@\S+?)\s.*` – chris85 May 28 '15 at 16:20
  • Perhaps you could explain why you need to do it this way as opposed to using filter_var()? – Kirk Powell May 28 '15 at 16:30
  • @chris85 its not just related to email. – user3815806 May 28 '15 at 16:46
  • @Luceos Could you please modify my regex code and make it work? Actually i really didnt understood how to write a better way. – user3815806 May 28 '15 at 16:48
  • 1
    Fixing your regex to work is a different question. – Luceos May 28 '15 at 17:22

1 Answers1

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.+_]+@[-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

Fabian N.
  • 3,807
  • 2
  • 23
  • 46
anubhava
  • 761,203
  • 64
  • 569
  • 643