10

I'm trying to get emails out of a string:

$string = "bla bla pickachu@domain.com MIME-Version: balbasur@domain.com bla bla bla";
$matches = array();
$pattern = '\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b';
preg_match_all($pattern,$string,$matches);
print_r($matches);

The error I'm getting is :

Delimiter must not be alphanumeric or backslash

I got the regex syntax from here http://www.regular-expressions.info/email.html

SherylHohman
  • 16,580
  • 17
  • 88
  • 94
homerun
  • 19,837
  • 15
  • 45
  • 70
  • Add /i or just / after last b( that is in pattern) – Shridhar Feb 24 '13 at 10:47
  • possible duplicate of [How to validate an email address in PHP](http://stackoverflow.com/questions/12026842/how-to-validate-an-email-address-in-php) – PeeHaa Feb 24 '13 at 16:43

3 Answers3

21

Like this

$pattern = '/[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b/i';

Or smaller version :)

$pattern = '/[a-z\d._%+-]+@[a-z\d.-]+\.[a-z]{2,4}\b/i';
Winston
  • 1,758
  • 2
  • 17
  • 29
  • works great , can you explain what causes the error and what you did to fix it? – homerun Feb 24 '13 at 10:53
  • 1
    @MorSela Yes, the first you need had to add delimiters // at start and end of the pattern. After need had to add "i" modificator for case insensitive. That's all. :) – Winston Feb 24 '13 at 10:56
  • thanks for that m8 , accepted asnwer :) – homerun Feb 24 '13 at 11:03
  • For those interested, this may help (Not my script, so not a plug). I swear by it since I found it, it's saved me so much time! - http://cybernetnews.com/online-regular-expression-builder/ – j5Dev Feb 24 '13 at 11:16
  • 1
    @j5Dev I also use regex builde, this one: http://www.regexbuddy.com/ very cool tools :) – Winston Feb 24 '13 at 11:19
  • @Winston hey Winston got 1 more question if you dont mind.. same question here as my main question , just about that regex : [a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])? (the error:Unknown modifier '=' ) thanks in advance! got that regex as well from that webpage - http://www.regular-expressions.info/email.html – homerun Feb 24 '13 at 18:24
  • 1
    @MorSela Do not mind :) Can you show how you're using this pattern in the function (`preg_match` or `preg_match_all`) – Winston Feb 24 '13 at 18:28
  • @winston preg_match_all , im basically using the same code as the main question , just testing different regex patterns (article author says thats this long regex is the best one so far so locate emails) :) – homerun Feb 24 '13 at 18:39
  • 1
    @MorSela I've corrected, your the regex pattern **#[a-z\d!\#$%&'*+/=?^_`{|}~-]+(?:\.[a-z\d!\#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z\d](?:[a-z\d-]*[a-z\d])?\.)+[a-z\d](?:[a-z\d-]*[a-z\d])?#** There is a caveat, for example regex pattern `/here your pattern/`, here you're using `/` pattern delimiter, therefore in your pattern you must escape each `/` char. Also, for example, if you're using `#` as pattern delimiter you must escape each `#` char in your pattern, etc... – Winston Feb 24 '13 at 19:01
  • You receive an error because used pattern delimiter in your pattern, without escaping it. – Winston Feb 24 '13 at 19:04
  • @Winston thank you for your answer, that's amazing! But now with the new TLD's you should change the 4 into 24. – ReeCube Sep 18 '18 at 10:44
2

When using PCRE regex functions it require to enclose the pattern by delimiters:

PHP Delimiters

Often used delimiters are forward slashes (/), hash signs (#) and tildes (~). The following are all examples of valid delimited patterns.

/foo bar/
#^[^0-9]$#
+php+
%[a-zA-Z0-9_-]%

Then you must correct this line to:

$pattern = '/\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b/';

or

$pattern = '#\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b#';
  • ive tried it before , $matches comes out empty.. – homerun Feb 24 '13 at 10:52
  • it about your pattern... change play with it... –  Feb 24 '13 at 10:52
  • @homerrrrrrrr In addition to adding proper delimiters, you would need to add the regex `i` flag, so the email search is case insensitive. The matches came out empty because it was using case sensitive search. The error you got is because the pattern was not properly wrapped in delimiters. The answer above *does* address the question, which was about an error message. A separate, 2nd, question would need to be asked if you then want to know why you got no matches. – SherylHohman Jan 03 '20 at 19:13
1

You just need to wrap your pattern in a proper delimiter, like forward slashes. Like so:

$pattern = '/\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b/';
Ian McMahon
  • 1,660
  • 11
  • 13