Archive for the ‘Regex’ Category
Masked TextBox in C#
Masked TextBoxes are those that won’t allow (or that will allow) certain strings, numbers or characters or patterns. Such kind of control is already present in the C#.Net with the name MaskedTextBox. This tutorial will suggest another way of doing such a TextBox in simple steps.
We will be using Regex in this tutorial. Its suggested that you have a look at the keywords used in writing Regex patterns [ RegularExpressions ].
RegexPal is an online regex tester. Its really handy at times.
Follow these steps to get a custom made masked textbox. We will be using Regex for specifying the mask
- Draw a TextBox in to your form
- Handle the KeyPress event of the TextBox
- Have a Regex object initialised to the mask pattern
- In the KeyPress handler use the Handled property of the KeyPressEvent to decide on whether to accept the key that has been pressed or not
Make sure that your Regex has ^ and $ at the beginning and end of the pattern
For example,
using System.Windows.Forms;
TextBox myCustomMaskedTextBox = new TextBox();
this.Controls.Add(myCustomMaskedTextBox);
Regex myPattern = new Regex(@"^[0-9]*$");
myCustomMaskedTextBox.KeyPress += new System.Windows.Forms.KeyPressEventHandler(CustomTextBoxKeyPressed);
private void CustomTextBoxKeyPressed(object sender, KeyPressEventArgs args)
{
//to allow the backspace key for deletion of the characters
if(args.KeyChar = '\b')
{
string t= myCustomMaskedTextBox.Text + args.KeyChar.ToString();
if(myRegex.IsMatch(t))
{
//pass
}
else
{
args.Handled = true;
}
}
}
The above snippet will allow the user to enter only numbers in to the textbox. Nothing else (except backspace)
Regular Expressions in C#
RegularExpressions (AKA: Regex) are special strings that define a search pattern. Writing regex in C# is much similar to those you write in python. Some of the important keywords to be learnt before going in for Regex in C# are,
^ – denotes the start of the string
$ – denotes the end of the string
? – zero or one of the preceding element
* – zero or more of the preceding element
+ – one or more of the preceding element
\d – digits
\s – white space
\w – word (characters – including numbers)
\D – non digits
\S – non white space
\W – non word
{m,n} – between m and n times of the preceding element
{n} – n times of the preceding element
For example,
(foo){3} – will be a regex pattern and it will be matching the string “this is a foofoofoo” and it will not match “this is a foo foo foo”.
([+]{1})([\d]{2})-([\d]{2})-([\d]{8}) – will be matching +01-11-12345678
[\s](ROAD) – will be matching only ROAD in “BROAD ROAD” and not BROAD because it expects <whitespace> before “ROAD”
[a-zA-Z]* – will be matching any characters in the set a to z or A to Z, zero or more times
Using Regex in C#
- To use Regex in C#, you need a Regex object. So create one from the namespace, System.Text.RegularExpressions
- The constructor will take the pattern as string and the option that it needs to put in while doing the pattern matching process.
- After creating a Regex object we can search for the pattern in any input strings using the Regex.IsMatch() function. This function takes the input string and returns a boolean value based on the findings.
If you want to get the number of matches, you can use, Regex.Matches() which will return a MatchCollection object. Then to get the number of matches you can use MatchCollection.Count property.
For example,
using System.Text.RegularExpressions;
Regex mySearchPattern = new Regex("([+]{1})([\d]{2})-([\d]{2})-([\d]{8})");
string myInputString = "+01-11-12345678 \n +01-11-87654321";
if(mySearchPattern.IsMatch(myInputString))
{
MatchCollection aCollection = mySearchPattern.Matches(myInputString);
MessageBox.Show(aCollection.Count.ToString());
}
The above snippet will give an output as 2