メールアドレスかどうか調べる

先に言っておきますが、正規表現でメールアドレスかどうかを調べるのは大変難しく、少なくともメールアドレスらしいもので振り分けます。

if (preg_match("/^([*+!.&#$|'¥¥%¥/0-9a-z^_`{}=?:-]+)@([0-9a-z-]+¥.)+[0-9a-z]{2,}$/i", $text)) {
    echo "正しいメールアドレスです";
} else {
    echo "不正なメールアドレスです";
}

より正確にしようとすると下記のように長くなりますが、RFC821,RFC822によるとメールアドレスはクオートすれば、ASCII文字なら(NULLを除く)なんでも使えます。

[¥040¥t]*(?:¥([^¥¥¥x80-¥xff¥n¥015()]*(?:(?:¥¥[^¥x80-¥xff]|¥([^¥¥¥x
80-¥xff¥n¥015()]*(?:¥¥[^¥x80-¥xff][^¥¥¥x80-¥xff¥n¥015()]*)*¥))[^¥¥
¥x80-¥xff¥n¥015()]*)*¥)[¥040¥t]*)*(?:(?:[^(¥040)<>@,;:".¥¥¥[¥]¥000
-¥037¥x80-¥xff]+(?![^(¥040)<>@,;:".¥¥¥[¥]¥000-¥037¥x80-¥xff])|"[^¥
¥¥x80-¥xff¥n¥015"]*(?:¥¥[^¥x80-¥xff][^¥¥¥x80-¥xff¥n¥015"]*)*")[¥04
0¥t]*(?:¥([^¥¥¥x80-¥xff¥n¥015()]*(?:(?:¥¥[^¥x80-¥xff]|¥([^¥¥¥x80-¥
xff¥n¥015()]*(?:¥¥[^¥x80-¥xff][^¥¥¥x80-¥xff¥n¥015()]*)*¥))[^¥¥¥x80
-¥xff¥n¥015()]*)*¥)[¥040¥t]*)*(?:¥.[¥040¥t]*(?:¥([^¥¥¥x80-¥xff¥n¥0
15()]*(?:(?:¥¥[^¥x80-¥xff]|¥([^¥¥¥x80-¥xff¥n¥015()]*(?:¥¥[^¥x80-¥x
ff][^¥¥¥x80-¥xff¥n¥015()]*)*¥))[^¥¥¥x80-¥xff¥n¥015()]*)*¥)[¥040¥t]
*)*(?:[^(¥040)<>@,;:".¥¥¥[¥]¥000-¥037¥x80-¥xff]+(?![^(¥040)<>@,;:"
.¥¥¥[¥]¥000-¥037¥x80-¥xff])|"[^¥¥¥x80-¥xff¥n¥015"]*(?:¥¥[^¥x80-¥xf
f][^¥¥¥x80-¥xff¥n¥015"]*)*")[¥040¥t]*(?:¥([^¥¥¥x80-¥xff¥n¥015()]*(
?:(?:¥¥[^¥x80-¥xff]|¥([^¥¥¥x80-¥xff¥n¥015()]*(?:¥¥[^¥x80-¥xff][^¥¥
¥x80-¥xff¥n¥015()]*)*¥))[^¥¥¥x80-¥xff¥n¥015()]*)*¥)[¥040¥t]*)*)*@[
¥040¥t]*(?:¥([^¥¥¥x80-¥xff¥n¥015()]*(?:(?:¥¥[^¥x80-¥xff]|¥([^¥¥¥x8
0-¥xff¥n¥015()]*(?:¥¥[^¥x80-¥xff][^¥¥¥x80-¥xff¥n¥015()]*)*¥))[^¥¥¥
x80-¥xff¥n¥015()]*)*¥)[¥040¥t]*)*(?:[^(¥040)<>@,;:".¥¥¥[¥]¥000-¥03
7¥x80-¥xff]+(?![^(¥040)<>@,;:".¥¥¥[¥]¥000-¥037¥x80-¥xff])|¥[(?:[^¥
¥¥x80-¥xff¥n¥015¥[¥]]|¥¥[^¥x80-¥xff])*¥])[¥040¥t]*(?:¥([^¥¥¥x80-¥x
ff¥n¥015()]*(?:(?:¥¥[^¥x80-¥xff]|¥([^¥¥¥x80-¥xff¥n¥015()]*(?:¥¥[^¥
x80-¥xff][^¥¥¥x80-¥xff¥n¥015()]*)*¥))[^¥¥¥x80-¥xff¥n¥015()]*)*¥)[¥
040¥t]*)*(?:¥.[¥040¥t]*(?:¥([^¥¥¥x80-¥xff¥n¥015()]*(?:(?:¥¥[^¥x80-
¥xff]|¥([^¥¥¥x80-¥xff¥n¥015()]*(?:¥¥[^¥x80-¥xff][^¥¥¥x80-¥xff¥n¥01
5()]*)*¥))[^¥¥¥x80-¥xff¥n¥015()]*)*¥)[¥040¥t]*)*(?:[^(¥040)<>@,;:"
.¥¥¥[¥]¥000-¥037¥x80-¥xff]+(?![^(¥040)<>@,;:".¥¥¥[¥]¥000-¥037¥x80-
¥xff])|¥[(?:[^¥¥¥x80-¥xff¥n¥015¥[¥]]|¥¥[^¥x80-¥xff])*¥])[¥040¥t]*(
?:¥([^¥¥¥x80-¥xff¥n¥015()]*(?:(?:¥¥[^¥x80-¥xff]|¥([^¥¥¥x80-¥xff¥n¥
015()]*(?:¥¥[^¥x80-¥xff][^¥¥¥x80-¥xff¥n¥015()]*)*¥))[^¥¥¥x80-¥xff¥
n¥015()]*)*¥)[¥040¥t]*)*)*|(?:[^(¥040)<>@,;:".¥¥¥[¥]¥000-¥037¥x80-
¥xff]+(?![^(¥040)<>@,;:".¥¥¥[¥]¥000-¥037¥x80-¥xff])|"[^¥¥¥x80-¥xff
¥n¥015"]*(?:¥¥[^¥x80-¥xff][^¥¥¥x80-¥xff¥n¥015"]*)*")[^()<>@,;:".¥¥
¥[¥]¥x80-¥xff¥000-¥010¥012-¥037]*(?:(?:¥([^¥¥¥x80-¥xff¥n¥015()]*(?
:(?:¥¥[^¥x80-¥xff]|¥([^¥¥¥x80-¥xff¥n¥015()]*(?:¥¥[^¥x80-¥xff][^¥¥¥
x80-¥xff¥n¥015()]*)*¥))[^¥¥¥x80-¥xff¥n¥015()]*)*¥)|"[^¥¥¥x80-¥xff¥
n¥015"]*(?:¥¥[^¥x80-¥xff][^¥¥¥x80-¥xff¥n¥015"]*)*")[^()<>@,;:".¥¥¥
[¥]¥x80-¥xff¥000-¥010¥012-¥037]*)*<[¥040¥t]*(?:¥([^¥¥¥x80-¥xff¥n¥0
15()]*(?:(?:¥¥[^¥x80-¥xff]|¥([^¥¥¥x80-¥xff¥n¥015()]*(?:¥¥[^¥x80-¥x
ff][^¥¥¥x80-¥xff¥n¥015()]*)*¥))[^¥¥¥x80-¥xff¥n¥015()]*)*¥)[¥040¥t]
*)*(?:@[¥040¥t]*(?:¥([^¥¥¥x80-¥xff¥n¥015()]*(?:(?:¥¥[^¥x80-¥xff]|¥
([^¥¥¥x80-¥xff¥n¥015()]*(?:¥¥[^¥x80-¥xff][^¥¥¥x80-¥xff¥n¥015()]*)*
¥))[^¥¥¥x80-¥xff¥n¥015()]*)*¥)[¥040¥t]*)*(?:[^(¥040)<>@,;:".¥¥¥[¥]
¥000-¥037¥x80-¥xff]+(?![^(¥040)<>@,;:".¥¥¥[¥]¥000-¥037¥x80-¥xff])|
¥[(?:[^¥¥¥x80-¥xff¥n¥015¥[¥]]|¥¥[^¥x80-¥xff])*¥])[¥040¥t]*(?:¥([^¥
¥¥x80-¥xff¥n¥015()]*(?:(?:¥¥[^¥x80-¥xff]|¥([^¥¥¥x80-¥xff¥n¥015()]*
(?:¥¥[^¥x80-¥xff][^¥¥¥x80-¥xff¥n¥015()]*)*¥))[^¥¥¥x80-¥xff¥n¥015()
]*)*¥)[¥040¥t]*)*(?:¥.[¥040¥t]*(?:¥([^¥¥¥x80-¥xff¥n¥015()]*(?:(?:¥
¥[^¥x80-¥xff]|¥([^¥¥¥x80-¥xff¥n¥015()]*(?:¥¥[^¥x80-¥xff][^¥¥¥x80-¥
xff¥n¥015()]*)*¥))[^¥¥¥x80-¥xff¥n¥015()]*)*¥)[¥040¥t]*)*(?:[^(¥040
)<>@,;:".¥¥¥[¥]¥000-¥037¥x80-¥xff]+(?![^(¥040)<>@,;:".¥¥¥[¥]¥000-¥
037¥x80-¥xff])|¥[(?:[^¥¥¥x80-¥xff¥n¥015¥[¥]]|¥¥[^¥x80-¥xff])*¥])[¥
040¥t]*(?:¥([^¥¥¥x80-¥xff¥n¥015()]*(?:(?:¥¥[^¥x80-¥xff]|¥([^¥¥¥x80
-¥xff¥n¥015()]*(?:¥¥[^¥x80-¥xff][^¥¥¥x80-¥xff¥n¥015()]*)*¥))[^¥¥¥x
80-¥xff¥n¥015()]*)*¥)[¥040¥t]*)*)*(?:,[¥040¥t]*(?:¥([^¥¥¥x80-¥xff¥
n¥015()]*(?:(?:¥¥[^¥x80-¥xff]|¥([^¥¥¥x80-¥xff¥n¥015()]*(?:¥¥[^¥x80
-¥xff][^¥¥¥x80-¥xff¥n¥015()]*)*¥))[^¥¥¥x80-¥xff¥n¥015()]*)*¥)[¥040
¥t]*)*@[¥040¥t]*(?:¥([^¥¥¥x80-¥xff¥n¥015()]*(?:(?:¥¥[^¥x80-¥xff]|¥
([^¥¥¥x80-¥xff¥n¥015()]*(?:¥¥[^¥x80-¥xff][^¥¥¥x80-¥xff¥n¥015()]*)*
¥))[^¥¥¥x80-¥xff¥n¥015()]*)*¥)[¥040¥t]*)*(?:[^(¥040)<>@,;:".¥¥¥[¥]
¥000-¥037¥x80-¥xff]+(?![^(¥040)<>@,;:".¥¥¥[¥]¥000-¥037¥x80-¥xff])|
¥[(?:[^¥¥¥x80-¥xff¥n¥015¥[¥]]|¥¥[^¥x80-¥xff])*¥])[¥040¥t]*(?:¥([^¥
¥¥x80-¥xff¥n¥015()]*(?:(?:¥¥[^¥x80-¥xff]|¥([^¥¥¥x80-¥xff¥n¥015()]*
(?:¥¥[^¥x80-¥xff][^¥¥¥x80-¥xff¥n¥015()]*)*¥))[^¥¥¥x80-¥xff¥n¥015()
]*)*¥)[¥040¥t]*)*(?:¥.[¥040¥t]*(?:¥([^¥¥¥x80-¥xff¥n¥015()]*(?:(?:¥
¥[^¥x80-¥xff]|¥([^¥¥¥x80-¥xff¥n¥015()]*(?:¥¥[^¥x80-¥xff][^¥¥¥x80-¥
xff¥n¥015()]*)*¥))[^¥¥¥x80-¥xff¥n¥015()]*)*¥)[¥040¥t]*)*(?:[^(¥040
)<>@,;:".¥¥¥[¥]¥000-¥037¥x80-¥xff]+(?![^(¥040)<>@,;:".¥¥¥[¥]¥000-¥
037¥x80-¥xff])|¥[(?:[^¥¥¥x80-¥xff¥n¥015¥[¥]]|¥¥[^¥x80-¥xff])*¥])[¥
040¥t]*(?:¥([^¥¥¥x80-¥xff¥n¥015()]*(?:(?:¥¥[^¥x80-¥xff]|¥([^¥¥¥x80
-¥xff¥n¥015()]*(?:¥¥[^¥x80-¥xff][^¥¥¥x80-¥xff¥n¥015()]*)*¥))[^¥¥¥x
80-¥xff¥n¥015()]*)*¥)[¥040¥t]*)*)*)*:[¥040¥t]*(?:¥([^¥¥¥x80-¥xff¥n
¥015()]*(?:(?:¥¥[^¥x80-¥xff]|¥([^¥¥¥x80-¥xff¥n¥015()]*(?:¥¥[^¥x80-
¥xff][^¥¥¥x80-¥xff¥n¥015()]*)*¥))[^¥¥¥x80-¥xff¥n¥015()]*)*¥)[¥040¥
t]*)*)?(?:[^(¥040)<>@,;:".¥¥¥[¥]¥000-¥037¥x80-¥xff]+(?![^(¥040)<>@
,;:".¥¥¥[¥]¥000-¥037¥x80-¥xff])|"[^¥¥¥x80-¥xff¥n¥015"]*(?:¥¥[^¥x80
-¥xff][^¥¥¥x80-¥xff¥n¥015"]*)*")[¥040¥t]*(?:¥([^¥¥¥x80-¥xff¥n¥015(
)]*(?:(?:¥¥[^¥x80-¥xff]|¥([^¥¥¥x80-¥xff¥n¥015()]*(?:¥¥[^¥x80-¥xff]
[^¥¥¥x80-¥xff¥n¥015()]*)*¥))[^¥¥¥x80-¥xff¥n¥015()]*)*¥)[¥040¥t]*)*
(?:¥.[¥040¥t]*(?:¥([^¥¥¥x80-¥xff¥n¥015()]*(?:(?:¥¥[^¥x80-¥xff]|¥([
^¥¥¥x80-¥xff¥n¥015()]*(?:¥¥[^¥x80-¥xff][^¥¥¥x80-¥xff¥n¥015()]*)*¥)
)[^¥¥¥x80-¥xff¥n¥015()]*)*¥)[¥040¥t]*)*(?:[^(¥040)<>@,;:".¥¥¥[¥]¥0
00-¥037¥x80-¥xff]+(?![^(¥040)<>@,;:".¥¥¥[¥]¥000-¥037¥x80-¥xff])|"[
^¥¥¥x80-¥xff¥n¥015"]*(?:¥¥[^¥x80-¥xff][^¥¥¥x80-¥xff¥n¥015"]*)*")[¥
040¥t]*(?:¥([^¥¥¥x80-¥xff¥n¥015()]*(?:(?:¥¥[^¥x80-¥xff]|¥([^¥¥¥x80
-¥xff¥n¥015()]*(?:¥¥[^¥x80-¥xff][^¥¥¥x80-¥xff¥n¥015()]*)*¥))[^¥¥¥x
80-¥xff¥n¥015()]*)*¥)[¥040¥t]*)*)*@[¥040¥t]*(?:¥([^¥¥¥x80-¥xff¥n¥0
15()]*(?:(?:¥¥[^¥x80-¥xff]|¥([^¥¥¥x80-¥xff¥n¥015()]*(?:¥¥[^¥x80-¥x
ff][^¥¥¥x80-¥xff¥n¥015()]*)*¥))[^¥¥¥x80-¥xff¥n¥015()]*)*¥)[¥040¥t]
*)*(?:[^(¥040)<>@,;:".¥¥¥[¥]¥000-¥037¥x80-¥xff]+(?![^(¥040)<>@,;:"
.¥¥¥[¥]¥000-¥037¥x80-¥xff])|¥[(?:[^¥¥¥x80-¥xff¥n¥015¥[¥]]|¥¥[^¥x80
-¥xff])*¥])[¥040¥t]*(?:¥([^¥¥¥x80-¥xff¥n¥015()]*(?:(?:¥¥[^¥x80-¥xf
f]|¥([^¥¥¥x80-¥xff¥n¥015()]*(?:¥¥[^¥x80-¥xff][^¥¥¥x80-¥xff¥n¥015()
]*)*¥))[^¥¥¥x80-¥xff¥n¥015()]*)*¥)[¥040¥t]*)*(?:¥.[¥040¥t]*(?:¥([^
¥¥¥x80-¥xff¥n¥015()]*(?:(?:¥¥[^¥x80-¥xff]|¥([^¥¥¥x80-¥xff¥n¥015()]
*(?:¥¥[^¥x80-¥xff][^¥¥¥x80-¥xff¥n¥015()]*)*¥))[^¥¥¥x80-¥xff¥n¥015(
)]*)*¥)[¥040¥t]*)*(?:[^(¥040)<>@,;:".¥¥¥[¥]¥000-¥037¥x80-¥xff]+(?!
[^(¥040)<>@,;:".¥¥¥[¥]¥000-¥037¥x80-¥xff])|¥[(?:[^¥¥¥x80-¥xff¥n¥01
5¥[¥]]|¥¥[^¥x80-¥xff])*¥])[¥040¥t]*(?:¥([^¥¥¥x80-¥xff¥n¥015()]*(?:
(?:¥¥[^¥x80-¥xff]|¥([^¥¥¥x80-¥xff¥n¥015()]*(?:¥¥[^¥x80-¥xff][^¥¥¥x
80-¥xff¥n¥015()]*)*¥))[^¥¥¥x80-¥xff¥n¥015()]*)*¥)[¥040¥t]*)*)*>)

参照元

スポンサーリンク

関連記事

スポンサーリンク

<演算子 小さい

ホームページ製作・web系アプリ系の製作案件募集中です。

上に戻る