qr code c# mvc Lesson 1: Forming Regular Expressions in C#

Create QR Code 2d barcode in C# Lesson 1: Forming Regular Expressions

Lesson 1: Forming Regular Expressions
Making Denso QR Bar Code In C#.NET
Using Barcode creator for .NET Control to generate, create QR Code ISO/IEC18004 image in Visual Studio .NET applications.
www.OnBarcode.com
Recognizing QR-Code In Visual C#.NET
Using Barcode decoder for VS .NET Control to read, scan read, scan image in VS .NET applications.
www.OnBarcode.com
Lab: Create a Regex Expression Evaluator
Encoding Bar Code In C#
Using Barcode printer for VS .NET Control to generate, create barcode image in VS .NET applications.
www.OnBarcode.com
Barcode Scanner In C#.NET
Using Barcode scanner for Visual Studio .NET Control to read, scan read, scan image in Visual Studio .NET applications.
www.OnBarcode.com
In this lab, you process an array of strings to distinguish valid phone numbers and Zip Codes. Then you will reformat the phone numbers. If you encounter a problem completing an exercise, the completed projects are available on the companion CD in the Code folder.
Generating Quick Response Code In .NET
Using Barcode creation for ASP.NET Control to generate, create QR Code 2d barcode image in ASP.NET applications.
www.OnBarcode.com
QR Code Generator In Visual Studio .NET
Using Barcode printer for .NET Control to generate, create QR Code 2d barcode image in .NET applications.
www.OnBarcode.com
Exercise 1: Distinguish Between a Phone Number and a Zip Code
QR Code Drawer In VB.NET
Using Barcode printer for VS .NET Control to generate, create Denso QR Bar Code image in VS .NET applications.
www.OnBarcode.com
Encoding UPC A In Visual C#.NET
Using Barcode drawer for Visual Studio .NET Control to generate, create UPC-A image in .NET framework applications.
www.OnBarcode.com
In this exercise, you write code to distinguish between a phone number, a Zip Code, and invalid data. 1. Copy either the Visual Basic or C# version of the 03\Lesson1-Exercise1 folder from the companion CD to your hard disk, and open the solution file. 2. Using one line of code, complete the IsPhone method so that it returns true if any of the following formats are provided:
Print Bar Code In C#.NET
Using Barcode creator for .NET framework Control to generate, create bar code image in .NET applications.
www.OnBarcode.com
Matrix 2D Barcode Generation In C#.NET
Using Barcode generation for .NET framework Control to generate, create Matrix 2D Barcode image in VS .NET applications.
www.OnBarcode.com
(555)555-1212 (555) 555-1212 555-555-1212 5555551212
Generating Code 128A In Visual C#.NET
Using Barcode creator for .NET Control to generate, create Code 128 Code Set A image in .NET applications.
www.OnBarcode.com
2/5 Interleaved Creation In Visual C#.NET
Using Barcode printer for .NET Control to generate, create ITF image in Visual Studio .NET applications.
www.OnBarcode.com
Though many different regular expressions would work, the IsPhone method you write could look like this:
Bar Code Creator In Visual Basic .NET
Using Barcode encoder for .NET framework Control to generate, create barcode image in Visual Studio .NET applications.
www.OnBarcode.com
Paint Code 128B In .NET
Using Barcode generation for ASP.NET Control to generate, create Code-128 image in ASP.NET applications.
www.OnBarcode.com
' VB Function IsPhone(ByVal s As String) As Boolean Return Regex.IsMatch(s, "^\( \d{3}\) [\s\-] \d{3}\- \d{4}$") End Function // C# static bool IsPhone(string s) { return Regex.IsMatch(s, @"^\( \d{3}\) [\s\-] \d{3}\- \d{4}$"); }
Making EAN-13 Supplement 5 In VS .NET
Using Barcode generation for .NET Control to generate, create EAN13 image in Visual Studio .NET applications.
www.OnBarcode.com
Data Matrix Creation In Visual Basic .NET
Using Barcode drawer for .NET Control to generate, create DataMatrix image in .NET framework applications.
www.OnBarcode.com
Each component of this regular expression matches a required or optional part of a phone number:
Code128 Encoder In Java
Using Barcode creation for Java Control to generate, create Code 128 Code Set C image in Java applications.
www.OnBarcode.com
Code-128 Maker In None
Using Barcode drawer for Office Word Control to generate, create ANSI/AIM Code 128 image in Office Word applications.
www.OnBarcode.com
^ Matches the beginning of the string. \( Optionally matches an opening parenthesis. The parenthesis is pre-
UPC A Generation In Objective-C
Using Barcode generator for iPad Control to generate, create UPC-A image in iPad applications.
www.OnBarcode.com
Read Barcode In VS .NET
Using Barcode Control SDK for ASP.NET Control to generate, create, read, scan barcode image in ASP.NET applications.
www.OnBarcode.com
ceded with a backslash, because the parenthesis is a special character in regular expressions. The following question mark makes the parenthesis optional.
3
Searching, Modifying, and Encoding Text
\d{3} Matches exactly three numeric digits. \) Optionally matches a closing parenthesis. The parenthesis is preceded
with a backslash because the parenthesis is a special character in regular expressions. The following question mark makes the parenthesis optional.
[\s\-] Matches either a space ( \s ) or a hyphen ( \- ) separating the area
code from the rest of the phone number. The following question mark makes the space or hyphen optional.
\d{3} Matches exactly three numeric digits. \- Optionally matches a hyphen. \d{4}$ Requires that the string end with four numeric digits.
3. Using one line of code, complete the IsZip method so that it returns true if any of the following formats are provided:
01111 01111-1111
Though many different regular expressions would work, the IsZip method you write could look like this:
' VB Function IsZip(ByVal s As String) As Boolean Return Regex.IsMatch(s, "^\d{5}(\-\d{4}) $") End Function // C# static bool IsZip(string s) { return Regex.IsMatch(s, @"^\d{5}(\-\d{4}) $"); }
Each component of this regular expression matches a required or optional part of a Zip Code:
^ Matches the beginning of the string. \d{5} Matches exactly five numeric digits. (\-\d{4}) Optionally matches a hyphen followed by exactly four numeric digits. Because the expression is surrounded by parentheses and followed by a question mark, the expression is considered optional. $
Matches the end of the string.
Lesson 1: Forming Regular Expressions
4. Build and run the project. The output should match the following:
(555)555-1212 is a phone number (555) 555-1212 is a phone number 555-555-1212 is a phone number 5555551212 is a phone number 01111 is a zip code 01111-1111 is a zip code 47 is unknown 111-11-1111 is unknown
If the output you get does not match the output just shown, adjust your regular expressions as needed.
Exercise 2: Reformat a String
In this exercise, you must reformat phone numbers into a standard (###) ###-#### format. 1. Open the project you created in Exercise 1. 2. Add a method named ReformatPhone that returns a string and accepts a single string as an argument. Using regular expressions, accept phone-number data provided in one of the formats used in Exercise 1, and reformat the data into the (###) ###-#### format. Though many different regular expressions would work, the IsZip method you write could look like this:
' VB Function ReformatPhone(ByVal s As String) As String Dim m As Match = Regex.Match(s, "^\( (\d{3})\) [\s\-] (\d{3})\- (\d{4})$") Return String.Format("({0}) {1}-{2}", m.Groups(1), m.Groups(2), m.Groups(3)) End Function // C# static string ReformatPhone(string s) { Match m = Regex.Match(s, @"^\( (\d{3})\) [\s\-] (\d{3})\- (\d{4})$"); return String.Format("({0}) {1}-{2}", m.Groups[1], m.Groups[2], m.Groups[3]); }
Notice that this regular expression almost exactly matches that used in the IsPhone method. The only difference is that each of the \d{n} expressions is surrounded by parentheses. This places each of the sets of numbers into a separate group that can be easily formatted using String.Format. 3. Change the Main method so that it writes ReformatPhone(s) in the foreach loop instead of simply s. For example, the foreach loop should now look like this:
Copyright © OnBarcode.com . All rights reserved.