I'm trying to make a regex to match email addresses, like any of these:
example@website.com
first.last@website.org
joe87_smith@web.net
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 = "test_last@test.com was the email address associated with another one, another.test@other.org";
$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) "test_last@test.com was"
[1] => string(22) "another.test@other.org"
}
[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...