Hello
Today I’ve downloaded the program expresso and made some teaching lessons on regular expressions. For me this was a very interesting lesson. Of course, I’ve had little experience in regular expressions but they are reduced on the modrewrite engine of the apache.
The technique of the regular expressions may be able to help us in a large amount of input problems in openpetra. Therefore I’ve written a small prototype of a RegularExpessionTextbox which can replace the Textboxes to insert the date and the currency and something more.
The interesting point is that you can name and later identify a group that matches to a specific pattern. Let us have a look to the string containing "?<Month>\d{1,2}/(?<Day>\d{1,2})/(?<Year>(?:\d{4}|\d{2}))$". This pattern identifies one date in us format which can be inserted in two variants. The year can be defined in a two digit mode and a four digit mode. So the very first program converts the result of the pattern matching in the following way.
string strOutputFormat ="dd-MMM-yyyy";
private string ConvertInput(Match match)
{
int year = int.Parse(match.Groups["Year"].ToString());
int month = int.Parse(match.Groups["Month"].ToString());
int day = int.Parse(match.Groups["Day"].ToString());
if (year < 99) {
year = year + 2000;
}
return new DateTime(year, month, day).ToString(strOutputFormat).ToUpper();
}
The values are parsed and joined to a DateTime-Object and this itself is converted to a string and the conversion is controlled by a format string.
So from the view of the software we only need this routine and from the view of the i18n we need the string containing the matching patterns and the string to format the output.
The we can read in everything – E-Mails, post-codes, Bank-Account numbers - any form of input we can handle.
[]one control[/:m]
[]one problem specific function[/:m]
[]one expression string[/:m]
[]one format string[/:m][/list:u]
And expresson delivers a lot of examples and a regex analyzer.
My idea is to reduce the differnt controls down to one control.
Best regards
Wolfgang
Complete Prototype Program ...
public partial class RegularExpressionTextBox : TextBox
{
string strOutputFormat ="dd-MMM-yyyy";
string strRegExpression = "^(?<Month>\\d{1,2})/(?<Day>\\d{1,2})/(?<Year>(?:\\d{4}|\\d{2}))$";
Regex regex;
public RegularExpressionTextBox()
{
regex = new Regex(
strRegExpression,
RegexOptions.IgnoreCase
| RegexOptions.CultureInvariant
| RegexOptions.IgnorePatternWhitespace
| RegexOptions.Compiled
);
this.Validating += new CancelEventHandler(TextBoxValidating);
this.Validated += new System.EventHandler(TextBoxIsValidated);
}
public void TextBoxValidating(object obj, CancelEventArgs eva)
{
string strInputText = this.Text;
if (!strInputText.Equals(""))
{
try {
ConvertInput(regex.Matches(strInputText)[0]);
} catch
{
this.BackColor = Color.Pink;
eva.Cancel = true;
}
}
}
public void TextBoxIsValidated(object obj, EventArgs eva)
{
string strInputText = this.Text;
if (!strInputText.Equals(""))
{
this.Text = ConvertInput(regex.Matches(strInputText)[0]);
}
this.BackColor = Color.Empty;
}
private string ConvertInput(Match match)
{
int year = int.Parse(match.Groups["Year"].ToString());
int month = int.Parse(match.Groups["Month"].ToString());
int day = int.Parse(match.Groups["Day"].ToString());
if (year < 99) {
year = year + 2000;
}
return new DateTime(year, month, day).ToString(strOutputFormat).ToUpper();
}
}