getting emails out of string - regex syntax + preg_match_all [closed]
Asked Answered
B

3

10

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

$string = "bla bla [email protected] MIME-Version: [email protected] 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

Bathesda answered 24/2, 2013 at 10:40 Comment(2)
Add /i or just / after last b( that is in pattern)Moua
possible duplicate of How to validate an email address in PHPKnowling
G
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';
Glen answered 24/2, 2013 at 10:45 Comment(11)
works great , can you explain what causes the error and what you did to fix it?Bathesda
@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. :)Glen
thanks for that m8 , accepted asnwer :)Bathesda
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! - cybernetnews.com/online-regular-expression-builderBs
@Bs I also use regex builde, this one: regexbuddy.com very cool tools :)Glen
@Glen 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 - regular-expressions.info/email.htmlBathesda
@MorSela Do not mind :) Can you show how you're using this pattern in the function (preg_match or preg_match_all)Glen
@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) :)Bathesda
@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...Glen
You receive an error because used pattern delimiter in your pattern, without escaping it.Glen
@Glen thank you for your answer, that's amazing! But now with the new TLD's you should change the 4 into 24.Anchie
E
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#';
Ellamaeellan answered 24/2, 2013 at 10:45 Comment(3)
ive tried it before , $matches comes out empty..Bathesda
it about your pattern... change play with it...Ellamaeellan
@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.Capybara
S
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/';
Siward answered 24/2, 2013 at 10:44 Comment(2)
ive tried it before , $matches comes out empty..Bathesda
in that case, your regex needs adjusting. The syntax error is fixed by delimiting properly; constructing a valid regex is a whole different matter :)Siward

© 2022 - 2024 — McMap. All rights reserved.