0

My regex is:

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

My content among others is:

Q: Email Address 
A: name@example.com

Rad Software Regular Expression Designer says that it should work.

Various online sites return the correct results.

If I remove the (?<=Α: ) lookbehind the regex returns all emails correctly.

When I run it from php it returns no matches.

What's going on?

I've also used the specific type of regex (ie (?<=Email: ) with different content. It works just fine in that case.

Toby Allen
  • 10,997
  • 11
  • 73
  • 124
jimmy
  • 504
  • 8
  • 16

5 Answers5

1

This is my newer monster script for verifying whether an e-mail "validates" or not. You can feed it strange things and break it, but in production this handles 99.99999999% of the problems I've encountered. A lot more false positives really from typos.

<?php

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

$examples = array(
  'email@email.com',
  'my.email@email.com',
  'e.mail.more@email.co.uk',
  'bad.email@..email.com',
  'bad.email@google',
  '@google.com',
  'my@email@my.com',
  'my email@my.com',
);


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

?>

Output

  1. email@email.com - passes
  2. my.email@email.com - passes
  3. e.mail.more@email.co.uk - passes
  4. bad.email@..email.com - fails
  5. bad.email@google - fails
  6. @google.com - fails
  7. my@email@my.com - fails
  8. my email@my.com - fails

Unless there's a reason for the look-behind, you can match all of the emails in the string with preg_match_all(). Since you're working with a string, you would slightly modify the regex slightly:

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

$mystring = '
email@email.com - passes
my.email@email.com - passes
e.mail.more@email.co.uk - passes
bad.email@..email.com - fails
bad.email@google - fails
@google.com - fails
my@email@my.com - fails
my email@my.com - fails
';

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

print_r ($matches[1]);

Output from string only

Array
(
    [0] => email@email.com
    [1] => my.email@email.com
    [2] => e.mail.more@email.co.uk
    [3] => email@my.com
)
AbsoluteƵERØ
  • 7,816
  • 2
  • 24
  • 35
1

You are not most likely not using DOTALL flag s here which will make DOT match newlines as well in your regex:

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

OUTPUT:

Array
(
    [0] => Array
        (
            [0] => name@example.com
        )

    [1] => Array
        (
            [0] => name@example.com
        )

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

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

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

)
anubhava
  • 761,203
  • 64
  • 569
  • 643
0

Outside of your regex issue itself, you should really consider not trying to write your own e-mail address regex parser. See stackoverflow post: Using a regular expression to validate an email address on why -- upshot: the RFC is long and demanding on your regex abilities.

Community
  • 1
  • 1
0

The problem is that your regular expression contains Α, which has an accent over it, but the content contains A, which doesn't. So the lookbehind doesn't match.

I change the regex to:

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

and it works.

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • You have got to be kidding me.... An hour spent trying to find out what's wrong and in the end I'm looking for a Greek 'A' when I want an English one... – jimmy Apr 25 '13 at 20:20
0

The A char in your subject is the "normal" char with the code 65 (unicode or ascii). But The A you use in the lookbehind of your pattern have the code 913 (unicode). They look similar but are different.

Casimir et Hippolyte
  • 88,009
  • 5
  • 94
  • 125