翻訳への提案を行います
 
他のユーザーによる提案:

progress indicator
他の提案はありません。
クリックして評価とフィードバックをお寄せください
MSDN
MSDN ライブラリ
.NET 開発
.NET Framework 4
.NET Framework の基本開発
開発の基本
基本型の操作
文字列の操作
.NET Framework の正規表現
正規表現の例
 方法: 文字列が有効な電子メール形式であるかどうかを検証する
すべて縮小/すべて展開 すべて縮小
コンテンツの表示:   英語と日本語を並べて表示コンテンツの表示: 英語と日本語を並べて表示
.NET Framework 4
How to: Verify That Strings Are in Valid E-Mail Format

The following example verifies that a string is in valid e-email format.

The example defines an IsValidEmail method, which returns true if the string contains a valid e-mail address and false if it does not, but takes no other action. To verify that the e-mail address is valid, the method calls the Regex..::.IsMatch(String, String) method to verify that the address conforms to a regular expression pattern. You can use IsValidEmail to filter out e-mail addresses that contain invalid characters before your application stores the addresses in a database or displays them in an ASP.NET page.

Note that the IsValidEmail method does not perform authentication to validate the e-mail address. It merely determines whether its format is valid for an e-mail address.

Visual Basic
Imports System.Text.RegularExpressions

Module RegexUtilities
   Function IsValidEmail(strIn As String) As Boolean
       ' Return true if strIn is in valid e-mail format.
       Return Regex.IsMatch(strIn, _
              "^(?("")("".+?""@)|(([0-9a-zA-Z]((\.(?!\.))|[-!#\$%&'\*\+/=\?\^`\{\}\|~\w])*)(?<=[0-9a-zA-Z])@))" + _
              "(?(\[)(\[(\d{1,3}\.){3}\d{1,3}\])|(([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,6}))$")
   End Function
End Module
C#
using System;
using System.Text.RegularExpressions;

public class RegexUtilities
{
   public static bool IsValidEmail(string strIn)
   {
       // Return true if strIn is in valid e-mail format.
       return Regex.IsMatch(strIn, 
              @"^(?("")("".+?""@)|(([0-9a-zA-Z]((\.(?!\.))|[-!#\$%&'\*\+/=\?\^`\{\}\|~\w])*)(?<=[0-9a-zA-Z])@))" + 
              @"(?(\[)(\[(\d{1,3}\.){3}\d{1,3}\])|(([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,6}))$"); 
   }
}

In this example, the regular expression pattern ^(?("")("".+?""@)|(([0-9a-zA-Z]((\.(?!\.))|[-!#\$%&'\*\+/=\?\^`\{\}\|~\w])*)(?<=[0-9a-zA-Z])@))(?(\[)(\[(\d{1,3}\.){3}\d{1,3}\])|(([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,6}))$ can be interpreted as shown in the following table.

Pattern

Description

^

Begin the match at the start of the string.

(?("")

Determine whether the first character is a quotation mark. (?("") is the beginning of an alternation construct.

((?("")("".+?""@)

If the first character is a quotation mark, match a beginning quotation mark followed by at least one occurrence of any character followed by an ending quotation mark. The string should conclude with an at sign (@).

|(([0-9a-zA-Z]

If the first character is not a quotation mark, match any alphabetic character from a to z or any numeric character from 0 to 9.

(\.(?! \.))

If the next character is a period, match it. If it is not a period, look ahead to the next character and continue the match. (?!\.) is a zero-width negative lookahead assertion that prevents two consecutive periods from appearing in the local part of an e-mail address.

|[-!#\$%&'\*\+/=\? \^`\{\}\|~\w]

If the next character is not a period, match any word character or one of the following characters: -!#$%'*+=?^`{}|~.

((\.(?! \.))|[-!#\$%'\*\+/=\? \^`\{\}\|~\w])*

Match the alternation pattern (a period followed by a non-period, or one of a number of characters) zero or more times.

@

Match the @ character.

(?<=[0-9a-zA-Z])

Continue the match if the character that precedes the @ character is A through Z, a through z, or 0 through 9. The (?<=[0-9a-zA-Z]) construct defines a zero-width positive lookbehind assertion.

(?(\[)

Check whether the character that follows @ is an opening bracket.

(\[(\d{1,3}\.){3}\d{1,3}\])

If it is an opening bracket, match the opening bracket followed by an IP address (four sets of one to three digits, with each set separated by a period) and a closing bracket.

|(([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,6})

If the character that follows @ is not an opening bracket, match one alphanumeric character with a value of A-Z, a-z, or 0-9, followed by zero or more occurrences of a word character or a hyphen, followed by an alphanumeric character with a value of A-Z, a-z, or 0-9, followed by a period. This pattern can be repeated one or more times, and should be followed by two to six alphabetic (a-z, A-Z) characters. This portion of the regular expression is designed to capture the domain name.

The IsValidEmail method can be included in a library of regular expression utility methods, or it could be included as a private static or instance method in the application class. If it is used as a static method in a regular expression library, it can be called by using code such as the following:

Visual Basic
Public Class Application
   Public Shared Sub Main()
      Dim emailAddresses() As String = { "david.jones@proseware.com", "d.j@server1.proseware.com", _
                                         "jones@ms1.proseware.com", "j.@server1.proseware.com", _
                                         "j@proseware.com9", "js#internal@proseware.com", _
                                         "j_9@[129.126.118.1]", "j..s@proseware.com", _
                                         "js*@proseware.com", "js@proseware..com", _
                                         "js@proseware.com9", "j.s@server1.proseware.com" }

      For Each emailAddress As String In emailAddresses
         If RegexUtilities.IsValidEmail(emailAddress) Then
            Console.WriteLine("Valid: {0}", emailAddress)
         Else
            Console.WriteLine("Invalid: {0}", emailAddress)
         End If      
      Next                                            
   End Sub
End Class
' The example displays the following output:
'       Valid: david.jones@proseware.com
'       Valid: d.j@server1.proseware.com
'       Valid: jones@ms1.proseware.com
'       Invalid: j.@server1.proseware.com
'       Invalid: j@proseware.com9
'       Valid: js#internal@proseware.com
'       Valid: j_9@[129.126.118.1]
'       Invalid: j..s@proseware.com
'       Invalid: js*@proseware.com
'       Invalid: js@proseware..com
'       Invalid: js@proseware.com9
'       Valid: j.s@server1.proseware.com
C#
public class Application
{
   public static void Main()
   {
      string[] emailAddresses = { "david.jones@proseware.com", "d.j@server1.proseware.com", 
                                  "jones@ms1.proseware.com", "j.@server1.proseware.com", 
                                  "j@proseware.com9", "js#internal@proseware.com", 
                                  "j_9@[129.126.118.1]", "j..s@proseware.com", 
                                  "js*@proseware.com", "js@proseware..com", 
                                  "js@proseware.com9", "j.s@server1.proseware.com" };
      foreach (string emailAddress in emailAddresses)
      {
         if (RegexUtilities.IsValidEmail(emailAddress))
            Console.WriteLine("Valid: {0}", emailAddress);
         else
            Console.WriteLine("Invalid: {0}", emailAddress);
      }                                            
   }
}
// The example displays the following output:
//       Valid: david.jones@proseware.com
//       Valid: d.j@server1.proseware.com
//       Valid: jones@ms1.proseware.com
//       Invalid: j.@server1.proseware.com
//       Invalid: j@proseware.com9
//       Valid: js#internal@proseware.com
//       Valid: j_9@[129.126.118.1]
//       Invalid: j..s@proseware.com
//       Invalid: js*@proseware.com
//       Invalid: js@proseware..com
//       Invalid: js@proseware.com9
//       Valid: j.s@server1.proseware.com
.NET Framework 4
方法: 文字列が有効な電子メール形式であるかどうかを検証する

文字列が有効な電子メール形式であるかどうかを検証する例を次に示します。

次の例では、IsValidEmail メソッドを定義します。このメソッドは、文字列に有効な電子メール アドレスが含まれている場合に true を返し、含まれていない場合に false を返します。それ以外の動作は行いません。 電子メール アドレスが有効であるかどうかを確認するため、このメソッドは Regex..::.IsMatch(String, String) メソッドを呼び出して、アドレスが正規表現パターンに準拠するかどうかを検査します。 IsValidEmail を使うと、アプリケーションでアドレスをデータベースに格納したり、ASP.NET ページに表示する前に、無効な文字を含む電子メール アドレスを排除できます。

IsValidEmail メソッドは、認証によって電子メール アドレスを検証するわけではありません。 電子メール アドレスの形式として有効かどうかをを判断しているだけです。

Visual Basic
Imports System.Text.RegularExpressions

Module RegexUtilities
   Function IsValidEmail(strIn As String) As Boolean
       ' Return true if strIn is in valid e-mail format.
       Return Regex.IsMatch(strIn, _
              "^(?("")("".+?""@)|(([0-9a-zA-Z]((\.(?!\.))|[-!#\$%&'\*\+/=\?\^`\{\}\|~\w])*)(?<=[0-9a-zA-Z])@))" + _
              "(?(\[)(\[(\d{1,3}\.){3}\d{1,3}\])|(([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,6}))$")
   End Function
End Module
C#
using System;
using System.Text.RegularExpressions;

public class RegexUtilities
{
   public static bool IsValidEmail(string strIn)
   {
       // Return true if strIn is in valid e-mail format.
       return Regex.IsMatch(strIn, 
              @"^(?("")("".+?""@)|(([0-9a-zA-Z]((\.(?!\.))|[-!#\$%&'\*\+/=\?\^`\{\}\|~\w])*)(?<=[0-9a-zA-Z])@))" + 
              @"(?(\[)(\[(\d{1,3}\.){3}\d{1,3}\])|(([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,6}))$"); 
   }
}

この例の正規表現パターン ^(?("")("".+?""@)|(([0-9a-zA-Z]((\.(?!\.))|[-!#\$%&'\*\+/=\?\^`\{\}\|~\w])*)(?<=[0-9a-zA-Z])@))(?(\[)(\[(\d{1,3}\.){3}\d{1,3}\])|(([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,6}))$ の意味を次の表に示します。

パターン

説明

^

文字列の先頭から照合を開始します。

(?("")

最初の文字が引用符であるかどうかを確認します。 (?("") は代替構成体の開始位置です。

((?("")("".+?""@)

最初の文字が引用符の場合は、先頭の引用符、それに続く 1 個以上の任意の文字、さらにその後に末尾の引用符が続くパターンと一致します。 文字列はアット マーク (@) で終わる必要があります。

|(([0-9a-zA-Z]

最初の文字が引用符でない場合は、a ~ z の任意の英字または 0 ~ 9 の任意の数字と一致します。

(\.(?! \.))

次の文字がピリオドの場合は、その文字と一致します。 ピリオドでない場合は、次の文字を先読みして照合を継続します。 (?!\.) は、2 つの連続するピリオドが電子メール アドレスのローカル部分に出現することを防ぐゼロ幅の負の先読みアサーションです。

|[-!#\$%&'\*\+/=\? \^`\{\}\|~\w]

次の文字がピリオドでない場合は、任意の単語文字または -!#$%'*+=?^`{}|~ のいずれかの文字と一致します。

((\.(?! \.))|[-!#\$%'\*\+/=\? \^`\{\}\|~\w])*

0 回以上の代替パターン (ピリオドとそれに続くピリオド以外の文字、または複数の文字のうちのいずれかの 1 文字) と一致します。

@

@ 文字と一致します。

(?<=[0-9a-zA-Z])

@ 文字の前にくる文字が A ~ Z、a ~ z、または 0 ~ 9 である場合に照合を継続します。 (?<=[0-9a-zA-Z]) 構成体は、ゼロ幅の正の後読みアサーションを定義します。

(?(\[)

@ に続く文字が左角かっこかどうかを確認します。

(\[(\d{1,3}\.){3}\d{1,3}\])

左角かっこの場合は、左角かっこ、それに続く IP アドレス (各セットがピリオドで区切られた、4 セットの 1 ~ 3 桁の数字)、および右角かっこと一致します。

|(([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,6})

@ に続く文字が左角かっこでない場合は、値が A ~ Z、a ~ z、または 0 ~ 9 の 1 つの英数字の後に、単語文字またはハイフンの 0 回以上の出現、A ~ Z、a ~ z、または 0 ~ 9 の値の英数字、さらにピリオドが続くパターンと一致します。 このパターンは 1 回以上繰り返すことができ、後に 6 つの英字 (a ~ z、A ~ Z) が続く必要があります。 正規表現のこの部分は、ドメイン名をキャプチャするようにデザインされています。

IsValidEmail メソッドは、正規表現ユーティリティ メソッドのライブラリに含めることができるほか、アプリケーション クラス内のプライベートな静的メソッドやインスタンス メソッドとして含めることもできます。 正規表現ライブラリ内の静的メソッドとして使用した場合は、次のようなコードを使用して呼び出すことができます。

Visual Basic
Public Class Application
   Public Shared Sub Main()
      Dim emailAddresses() As String = { "david.jones@proseware.com", "d.j@server1.proseware.com", _
                                         "jones@ms1.proseware.com", "j.@server1.proseware.com", _
                                         "j@proseware.com9", "js#internal@proseware.com", _
                                         "j_9@[129.126.118.1]", "j..s@proseware.com", _
                                         "js*@proseware.com", "js@proseware..com", _
                                         "js@proseware.com9", "j.s@server1.proseware.com" }

      For Each emailAddress As String In emailAddresses
         If RegexUtilities.IsValidEmail(emailAddress) Then
            Console.WriteLine("Valid: {0}", emailAddress)
         Else
            Console.WriteLine("Invalid: {0}", emailAddress)
         End If      
      Next                                            
   End Sub
End Class
' The example displays the following output:
'       Valid: david.jones@proseware.com
'       Valid: d.j@server1.proseware.com
'       Valid: jones@ms1.proseware.com
'       Invalid: j.@server1.proseware.com
'       Invalid: j@proseware.com9
'       Valid: js#internal@proseware.com
'       Valid: j_9@[129.126.118.1]
'       Invalid: j..s@proseware.com
'       Invalid: js*@proseware.com
'       Invalid: js@proseware..com
'       Invalid: js@proseware.com9
'       Valid: j.s@server1.proseware.com
C#
public class Application
{
   public static void Main()
   {
      string[] emailAddresses = { "david.jones@proseware.com", "d.j@server1.proseware.com", 
                                  "jones@ms1.proseware.com", "j.@server1.proseware.com", 
                                  "j@proseware.com9", "js#internal@proseware.com", 
                                  "j_9@[129.126.118.1]", "j..s@proseware.com", 
                                  "js*@proseware.com", "js@proseware..com", 
                                  "js@proseware.com9", "j.s@server1.proseware.com" };
      foreach (string emailAddress in emailAddresses)
      {
         if (RegexUtilities.IsValidEmail(emailAddress))
            Console.WriteLine("Valid: {0}", emailAddress);
         else
            Console.WriteLine("Invalid: {0}", emailAddress);
      }                                            
   }
}
// The example displays the following output:
//       Valid: david.jones@proseware.com
//       Valid: d.j@server1.proseware.com
//       Valid: jones@ms1.proseware.com
//       Invalid: j.@server1.proseware.com
//       Invalid: j@proseware.com9
//       Valid: js#internal@proseware.com
//       Valid: j_9@[129.126.118.1]
//       Invalid: j..s@proseware.com
//       Invalid: js*@proseware.com
//       Invalid: js@proseware..com
//       Invalid: js@proseware.com9
//       Valid: j.s@server1.proseware.com

その他の技術情報

コミュニティ コンテンツ   コミュニティ コンテンツとは
新しいコンテンツの追加 RSS  注釈
Processing
© 2012 Microsoft. All rights reserved. 使用条件 | 商標 | プライバシー
Page view tracker