..


Sponsored Links

Regular Expressions in C #

Article written by Vincent Gaglio
Page 1 of 4

Very often programmers are in the position of having to parse strings of characters. A typical case is when processing a text entered by a user to search - and eventually eliminate - certain characters.
To simplify this and other types of activity, appear to be very useful so-called regular expressions.

A regular expression consists of an "abstract model" of a string that is used to make comparisons in order to verify that the string compared fulfills certain formatting requirements. It 'can also use regular expressions to extract or to replace portions of text.

Let's see an example intended to show how these expressions. Using Visual Studio We create a simple console application (which we call TestRegExp) that takes two objects of type string as input and determines if the first of them (a regular expression) matches per second or less. Should be included in the System.Text.RegularExpressions namespace and make the comparison using the static System.Text.RegularExpressions.Regex.IsMatch






 using System.Text.RegularExpressions;

 





 namespace TestRegExp

 





 {

 





 ClasseTest class

 





 {

 





 static void Main (string [] args)

 





 {

 





 if (Regex.IsMatch (args [1], args [0]))

 





 Console.WriteLine ("The input in the format set.");

 





 else

 





 Console.WriteLine ("The input does not respect the format specified.");}}}



At this point, we execute the application as the first parameter and pass the regular expression "^ \ d {5} $" (we will see shortly the meaning of questions symbols) and second as the string "1234" or "12345". The output of the application in the case of the string "1234" should be "The input does not respect the format specified.", While in the case of "12345" input in the format set. "






 C: \> TestRegExp ^ \ d {5} $ 1234

 





 The input does not respect the defined format.

 







 C: \> TestRegExp ^ \ d {5} $ 12345

 





 The input in the format set.



This is because, as you already understood, the method IsMatch compares a regular expression with a string and returns the Boolean value true if the format is dictated by the first observed from the second, false otherwise. In the above example the regular expression "^ \ d {5} $" indicates that the string to be evaluated must be of exactly 5 numbers (in fact, the comparison with "1234" returns false).

More specifically the character "^" represents the beginning of the string, "\ d" indicates that the characters must be numeric, "{5}" indicates the length of the string and "$" represents the end of the string. If you remove the character "^" the meaning of regular expressions will change profoundly. In fact, the expression \ d {5} $ will always require the presence of five numbers in sequence but also validate the string "abcd12345" or "ciaociao12345" because its meaning is "string that ends with five sequential numbers."

A good rule to follow is to always put the character "^" at the beginning of a regular expression and the character "$" at the end thereof. This arrangement ensures that the input must exactly match the regular expression and not simply contain compatible with the same characters.

In the same category ...
E-Learning
Linux (Course) Linux (Course)
Complete guide to open-source system. From 49 €.
Paint Shop Pro (First) Paint Shop Pro (First)
Web graphics and photo editing with Corel PSP known. From 49 €.
Visual Basic 6 (Course) Visual Basic 6 (Course)
Make Desktop Applications with VB6. From 39 €.
Sponsored Links