I'm trying to make a regex to match email addresses, like any of these:
[email protected]
[email protected]
[email protected]
I've written this regex:
$pattern = "/[\-\.\_a-z0-9]+(\@){1}[\-\.\_a-zA-Z0-9]+(\.){1}[\-a-z0-9]+/i";
and here is some code that I am using to test it:
$str = "[email protected] was the email address associated with another one, [email protected]";
$pattern = "/[\-\.\_a-z0-9]+(\@){1}[\-\.\_a-zA-Z0-9]+(\.){1}[\-a-z0-9]+/i";
preg_match_all($pattern, $str, $matches);
var_dump($matches);
(The text between the emails is filler) It's supposed to do as follows:
- Check for a username that can include one or more periods, dashes, underscores, or alphanumeric characters.
- Check for one and only one (required) "@" sign.
- Check for a domain or any number of subdomains (alphanumeric + periods + dashes)
- Check for a period followed by alphanumeric or dash characters.
When I test the code above, I get this output:
array(3) {
[0] => array(2) {
[0] => string(22) "[email protected] was"
[1] => string(22) "[email protected]"
}
[1] => array(2) {
[0] => string(1) "@"
[1] => string(1) "@"
}
[2] => array(2) {
[0] => string(1) " "
[1] => string(1) "r"
}
}
Why is it matching so many other characters, such as single @ signs and the letter "r"? Why does the very first email contain the word was? I never tested for spaces to my knowledge...