Retrieving letters from a string - regex + preg_match_all syntax

I am trying to get emails from 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.-]+\.[AZ]{2,4}\b'; preg_match_all($pattern,$string,$matches); print_r($matches); 

im get error: Delimiter must not be alphanumeric or backslash

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

what should I do? thanks in advance!

+7
source share
3 answers

Like this

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

Or a smaller version :)

 $pattern = '/[az\d._%+-] +@ [az\d.-]+\.[az]{2,4}\b/i'; 
+16
source

You just need to wrap your template in the correct separator, such as slashes. For example:

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

When using the PCRE regular expression functions, you must enclose a delimited pattern:

PHP separators

Commonly used delimiters are slashes (/), hash signs (#), and tildes (~). Below are all examples of valid separation patterns.

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

Then you have to fix this line so that:

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

or

 $pattern = '#\b[A-Z0-9._%+-] +@ [A-Z0-9.-]+\.[AZ]{2,4}\b#'; 
+1
source

All Articles