C#
パターン1:メールアドレス検証—簡易版が現実解
出典: C# 正規表現の業務系基本 — メール / 電話 / 郵便番号 / 全角半角の入力検証5パターン — パターン1:メールアドレス検証—簡易版が現実解
using System.Text.RegularExpressions;
public static class EmailValidator
{
// ✅業務系の現実版(簡易・ほぼ十分)
private static readonly Regex EmailRegex = new Regex(
@"^[^@\s]+@[^@\s]+\.[^@\s]+$",
RegexOptions.Compiled);
public static bool IsValid(string email)
{
if (string.IsNullOrWhiteSpace(email))return false;
return EmailRegex.IsMatch(email);
}
}
//使い方
class Program
{
static void Main()
{
Console.WriteLine(EmailValidator.IsValid("user@example.com")); // True
Console.WriteLine(EmailValidator.IsValid("user.name@sub.co.jp")); // True
Console.WriteLine(EmailValidator.IsValid("invalid@@example")); // False
Console.WriteLine(EmailValidator.IsValid("no-at-sign.com")); // False
}
}
▸ 実行ボタンで結果を表示
Source収録記事
この snippet は記事の「パターン1:メールアドレス検証—簡易版が現実解」セクションに登場する。コードの前後の文脈・ハマりどころの解説は記事本文で。
同じ記事から
5 件using System.Text.RegularExpressions; public static class PhoneValidator {
▶ 実行可
パターン2:電話番号検証—国内+国際対応
#f2eebc57a171
using System.Text.RegularExpressions; public static class PostalCodeValidator {
▶ 実行可
パターン3:郵便番号検証— 7桁ハイフン形式
#da3fce3a8fab
using System.Text.RegularExpressions; public static class CharTypeValidator {
▶ 実行可
パターン4:全角半角判定— Unicode範囲
#50a671243a77
using System; using System.Text.RegularExpressions; public static class FormatValidator
▶ 実行可
パターン5:数値・日付フォーマット検証
#5f14a216eab8
using System.Diagnostics; using System.Text.RegularExpressions; public static class RegexPerformanceComparison
▶ 実行可
パターン6: RegexOptions.Compiledでパフォーマンス改善
#228fa557c142
