-
Notifications
You must be signed in to change notification settings - Fork 173
Expand file tree
/
Copy pathListing05.12.SpecifyingTheUsingDirectiveInsideANamespaceDeclaration.cs
More file actions
54 lines (47 loc) · 1.69 KB
/
Listing05.12.SpecifyingTheUsingDirectiveInsideANamespaceDeclaration.cs
File metadata and controls
54 lines (47 loc) · 1.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// Disabled because not attributes have not been covered
// and the attribute is not available in .NET 6.0.
#pragma warning disable SYSLIB1045 // Convert to 'GeneratedRegexAttribute'.
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter05.Listing05_12;
#region INCLUDE
// The using directive imports all types from the
// specified namespace into the entire file
using System.Text.RegularExpressions;
public class Program
{
public static void Main()
{
#region EXCLUDE
const string firstName = "FirstName";
const string initial = "Initial";
const string lastName = "LastName";
// Explaining regular expressions is beyond the
// scope of this book.
// See https://www.regular-expressions.info/ for
// more information.
const string pattern = $"""
(?<{firstName}>\w+)\s+((?<{initial}>\w)\.\s+)?(?<{lastName}>\w+)\s*
""";
Console.WriteLine(
"Enter your full name (e.g. Inigo T. Montoya): ");
string name = Console.ReadLine()!;
#endregion EXCLUDE
#region HIGHLIGHT
// No need to qualify RegEx type with
// System.Text.RegularExpressions because
// of the using directive above
Match match = Regex.Match(name, pattern);
#endregion HIGHLIGHT
#region EXCLUDE
if (match.Success)
{
Console.WriteLine(
$"{firstName}: {match.Groups[firstName]}");
Console.WriteLine(
$"{initial}: {match.Groups[initial]}");
Console.WriteLine(
$"{lastName}: {match.Groups[lastName]}");
}
#endregion EXCLUDE
}
}
#endregion INCLUDE