Skip to content

Commit 9e5e193

Browse files
authored
Tighten up CS/VB.NET regex email sample (#504)
* Update CS sample for regex email * Update VB.NET sample for regex email
1 parent 4a60fd6 commit 9e5e193

File tree

2 files changed

+115
-109
lines changed
  • snippets
    • csharp/VS_Snippets_CLR/RegularExpressions.Examples.Email/cs
    • visualbasic/VS_Snippets_CLR/RegularExpressions.Examples.Email/vb

2 files changed

+115
-109
lines changed

snippets/csharp/VS_Snippets_CLR/RegularExpressions.Examples.Email/cs/example4.cs

Lines changed: 57 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -5,76 +5,76 @@
55

66
public class RegexUtilities
77
{
8-
bool invalid = false;
8+
public static bool IsValidEmail(string email)
9+
{
10+
if (string.IsNullOrWhitespace(email))
11+
return false;
912

10-
public bool IsValidEmail(string strIn)
11-
{
12-
invalid = false;
13-
if (String.IsNullOrEmpty(strIn))
14-
return false;
13+
try
14+
{
15+
// Normalize the domain
16+
email = Regex.Replace(email, @"(@)(.+)$", DomainMapper,
17+
RegexOptions.None, TimeSpan.FromMilliseconds(200));
1518

16-
// Use IdnMapping class to convert Unicode domain names.
17-
try {
18-
strIn = Regex.Replace(strIn, @"(@)(.+)$", this.DomainMapper,
19-
RegexOptions.None, TimeSpan.FromMilliseconds(200));
20-
}
21-
catch (RegexMatchTimeoutException) {
22-
return false;
23-
}
19+
// Examines the domain part of the email and normalizes it.
20+
string DomainMapper(Match match)
21+
{
22+
// Use IdnMapping class to convert Unicode domain names.
23+
var idn = new IdnMapping();
2424

25-
if (invalid)
26-
return false;
25+
// Pull out and process domain name (throws ArgumentException on invalid)
26+
var domainName = idn.GetAscii(match.Groups[2].Value);
2727

28-
// Return true if strIn is in valid email format.
29-
try {
30-
return Regex.IsMatch(strIn,
28+
return match.Groups[1].Value + domainName;
29+
}
30+
}
31+
catch (RegexMatchTimeoutException e)
32+
{
33+
return false;
34+
}
35+
catch (ArgumentException e)
36+
{
37+
return false;
38+
}
39+
40+
try
41+
{
42+
return Regex.IsMatch(email,
3143
@"^(?("")("".+?(?<!\\)""@)|(([0-9a-z]((\.(?!\.))|[-!#\$%&'\*\+/=\?\^`\{\}\|~\w])*)(?<=[0-9a-z])@))" +
3244
@"(?(\[)(\[(\d{1,3}\.){3}\d{1,3}\])|(([0-9a-z][-0-9a-z]*[0-9a-z]*\.)+[a-z0-9][\-a-z0-9]{0,22}[a-z0-9]))$",
3345
RegexOptions.IgnoreCase, TimeSpan.FromMilliseconds(250));
34-
}
35-
catch (RegexMatchTimeoutException) {
36-
return false;
37-
}
38-
}
39-
40-
private string DomainMapper(Match match)
41-
{
42-
// IdnMapping class with default property values.
43-
IdnMapping idn = new IdnMapping();
44-
45-
string domainName = match.Groups[2].Value;
46-
try {
47-
domainName = idn.GetAscii(domainName);
48-
}
49-
catch (ArgumentException) {
50-
invalid = true;
51-
}
52-
return match.Groups[1].Value + domainName;
53-
}
46+
}
47+
catch (RegexMatchTimeoutException)
48+
{
49+
return false;
50+
}
51+
}
5452
}
5553
// </Snippet7>
5654

5755
// <Snippet8>
58-
public class Application
56+
class Program
5957
{
60-
public static void Main()
61-
{
62-
RegexUtilities util = new RegexUtilities();
63-
string[] emailAddresses = { "david.jones@proseware.com", "d.j@server1.proseware.com",
64-
"jones@ms1.proseware.com", "j.@server1.proseware.com",
65-
"j@proseware.com9", "js#internal@proseware.com",
66-
"j_9@[129.126.118.1]", "j..s@proseware.com",
67-
"js*@proseware.com", "js@proseware..com",
68-
"js@proseware.com9", "j.s@server1.proseware.com",
69-
"\"j\\\"s\\\"\"@proseware.com", "js@contoso.中国" };
58+
static void Main(string[] args)
59+
{
60+
string[] emailAddresses = { "david.jones@proseware.com", "d.j@server1.proseware.com",
61+
"jones@ms1.proseware.com", "j.@server1.proseware.com",
62+
"j@proseware.com9", "js#internal@proseware.com",
63+
"j_9@[129.126.118.1]", "j..s@proseware.com",
64+
"js*@proseware.com", "js@proseware..com",
65+
"js@proseware.com9", "j.s@server1.proseware.com",
66+
"\"j\\\"s\\\"\"@proseware.com", "js@contoso.中国" };
67+
68+
foreach (var emailAddress in emailAddresses)
69+
{
70+
if (RegexUtilities.IsValidEmail(emailAddress))
71+
Console.WriteLine($"Valid: {emailAddress}");
72+
else
73+
Console.WriteLine($"Invalid: {emailAddress}");
74+
}
7075

71-
foreach (var emailAddress in emailAddresses) {
72-
if (util.IsValidEmail(emailAddress))
73-
Console.WriteLine("Valid: {0}", emailAddress);
74-
else
75-
Console.WriteLine("Invalid: {0}", emailAddress);
76-
}
77-
}
76+
Console.ReadKey();
77+
}
7878
}
7979
// The example displays the following output:
8080
// Valid: david.jones@proseware.com

snippets/visualbasic/VS_Snippets_CLR/RegularExpressions.Examples.Email/vb/example4.vb

Lines changed: 58 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -6,67 +6,73 @@ Imports System.Globalization
66
Imports System.Text.RegularExpressions
77

88
Public Class RegexUtilities
9-
Dim invalid As Boolean = False
10-
11-
public Function IsValidEmail(strIn As String) As Boolean
12-
invalid = False
13-
If String.IsNullOrEmpty(strIn) Then Return False
14-
15-
' Use IdnMapping class to convert Unicode domain names.
16-
Try
17-
strIn = Regex.Replace(strIn, "(@)(.+)$", AddressOf Me.DomainMapper,
18-
RegexOptions.None, TimeSpan.FromMilliseconds(200))
19-
Catch e As RegexMatchTimeoutException
20-
Return False
21-
End Try
22-
23-
If invalid Then Return False
24-
25-
' Return true if strIn is in valid email format.
26-
Try
27-
Return Regex.IsMatch(strIn,
28-
"^(?("")("".+?(?<!\\)""@)|(([0-9a-z]((\.(?!\.))|[-!#\$%&'\*\+/=\?\^`\{\}\|~\w])*)(?<=[0-9a-z])@))" +
29-
"(?(\[)(\[(\d{1,3}\.){3}\d{1,3}\])|(([0-9a-z][-0-9a-z]*[0-9a-z]*\.)+[a-z0-9][\-a-z0-9]{0,22}[a-z0-9]))$",
30-
RegexOptions.IgnoreCase, TimeSpan.FromMilliseconds(250))
31-
Catch e As RegexMatchTimeoutException
32-
Return False
33-
End Try
34-
End Function
35-
36-
Private Function DomainMapper(match As Match) As String
37-
' IdnMapping class with default property values.
38-
Dim idn As New IdnMapping()
39-
40-
Dim domainName As String = match.Groups(2).Value
41-
Try
42-
domainName = idn.GetAscii(domainName)
43-
Catch e As ArgumentException
44-
invalid = True
45-
End Try
46-
Return match.Groups(1).Value + domainName
47-
End Function
9+
10+
Public Shared Function IsValidEmail(email As String) As Boolean
11+
12+
If String.IsNullOrWhiteSpace(email) Then Return False
13+
14+
' Use IdnMapping class to convert Unicode domain names.
15+
Try
16+
'Examines the domain part of the email and normalizes it.
17+
Dim DomainMapper =
18+
Function(match As Match) As String
19+
20+
'Use IdnMapping class to convert Unicode domain names.
21+
Dim idn = New IdnMapping
22+
23+
'Pull out and process domain name (throws ArgumentException on invalid)
24+
Dim domainName As String = idn.GetAscii(match.Groups(2).Value)
25+
26+
Return match.Groups(1).Value & domainName
27+
28+
End Function
29+
30+
'Normalize the domain
31+
email = Regex.Replace(email, "(@)(.+)$", DomainMapper,
32+
RegexOptions.None, TimeSpan.FromMilliseconds(200))
33+
34+
Catch e As RegexMatchTimeoutException
35+
Return False
36+
37+
Catch e As ArgumentException
38+
Return False
39+
40+
End Try
41+
42+
Try
43+
Return Regex.IsMatch(email,
44+
"^(?("")("".+?(?<!\\)""@)|(([0-9a-z]((\.(?!\.))|[-!#\$%&'\*\+/=\?\^`\{\}\|~\w])*)(?<=[0-9a-z])@))" +
45+
"(?(\[)(\[(\d{1,3}\.){3}\d{1,3}\])|(([0-9a-z][-0-9a-z]*[0-9a-z]*\.)+[a-z0-9][\-a-z0-9]{0,22}[a-z0-9]))$",
46+
RegexOptions.IgnoreCase, TimeSpan.FromMilliseconds(250))
47+
48+
Catch e As RegexMatchTimeoutException
49+
Return False
50+
51+
End Try
52+
53+
End Function
54+
4855
End Class
4956
' </Snippet7>
5057

5158
' <Snippet8>
5259
Public Class Application
5360
Public Shared Sub Main()
54-
Dim util As New RegexUtilities()
55-
Dim emailAddresses() As String = { "david.jones@proseware.com", "d.j@server1.proseware.com", _
56-
"jones@ms1.proseware.com", "j.@server1.proseware.com", _
57-
"j@proseware.com9", "js#internal@proseware.com", _
58-
"j_9@[129.126.118.1]", "j..s@proseware.com", _
59-
"js*@proseware.com", "js@proseware..com", _
60-
"js@proseware.com9", "j.s@server1.proseware.com",
61-
"""j\""s\""""@proseware.com", "js@contoso.中国" }
61+
Dim emailAddresses() As String = {"david.jones@proseware.com", "d.j@server1.proseware.com",
62+
"jones@ms1.proseware.com", "j.@server1.proseware.com",
63+
"j@proseware.com9", "js#internal@proseware.com",
64+
"j_9@[129.126.118.1]", "j..s@proseware.com",
65+
"js*@proseware.com", "js@proseware..com",
66+
"js@proseware.com9", "j.s@server1.proseware.com",
67+
"""j\""s\""""@proseware.com", "js@contoso.中国"}
6268

6369
For Each emailAddress As String In emailAddresses
64-
If util.IsValidEmail(emailAddress) Then
65-
Console.WriteLine("Valid: {0}", emailAddress)
70+
If RegexUtilities.IsValidEmail(emailAddress) Then
71+
Console.WriteLine($"Valid: {emailAddress}")
6672
Else
67-
Console.WriteLine("Invalid: {0}", emailAddress)
68-
End If
69-
Next
73+
Console.WriteLine($"Invalid: {emailAddress}")
74+
End If
75+
Next
7076
End Sub
7177
End Class
7278
' The example displays the following output:

0 commit comments

Comments
 (0)