Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version
Go to top

Enhanced String Handling II

, 16 Dec 2010
Overcoming limitations of: Enhanced String Handling
EnhancedStringEvaluate.zip
EnhancedStringEvaluate
bin
Debug
ConfigEvaluate.vshost.exe
ConfigEvaluate.vshost.exe.manifest
EnhancedStringEvaluate.dll
EnhancedStringEvaluate.pdb
ConcordPairEvaluate.csproj.vs10x
EnhancedStringEvaluate.csproj.user
EnhancedStringEvaluate.csproj.vs10x
obj
Debug
Decl
EnhancedStringEvaluate.dll
EnhancedStringEvaluate.pdb
DesignTimeResolveAssemblyReferencesInput.cache
EnhancedStringEvaluate.dll
EnhancedStringEvaluate.pdb
Refactor
ConfigEvaluate.dll
ResolveAssemblyReference.cache
TempPE
Properties
EvaluateTest
bin
Debug
EnhancedStringEvaluate.dll
EnhancedStringEvaluate.pdb
EvaluateSampleTest.dll
EvaluateSampleTest.pdb
EvaluateTest.dll
EvaluateTest.pdb
TestEvaluation.exe
TestEvaluation.pdb
EvaluateSampleTest.csproj.vs10x
EvaluateTest.csproj.vs10x
obj
Debug
Decl
EvaluateSampleTest.dll
EvaluateSampleTest.pdb
DesignTimeResolveAssemblyReferencesInput.cache
EvaluateSampleTest.dll
EvaluateSampleTest.pdb
EvaluateTest.dll
EvaluateTest.pdb
ResolveAssemblyReference.cache
TempPE
Properties
TestEvaluate
bin
Debug
abc.text
EnhancedStringEvaluate.dll
EnhancedStringEvaluate.pdb
TestEvaluate.exe
TestEvaluate.pdb
TestEvaluate.vshost.exe.manifest
TestEvaluation.exe
TestEvaluation.pdb
TestEvaluation.vshost.exe
TestEvaluation.vshost.exe.manifest
TestOnTheFlyConfig.vshost.exe
TestOnTheFlyConfig.vshost.exe.manifest
obj
Debug
Decl
TestEvaluation.exe
TestEvaluation.pdb
DesignTimeResolveAssemblyReferencesInput.cache
Refactor
ResolveAssemblyReference.cache
TempPE
TestEvaluate.exe
TestEvaluate.pdb
TestEvaluation.exe
TestEvaluation.pdb
ProcessEvaluate
Properties
TestEvaluate.csproj.vs10x
TestEvaluation.csproj.user
TestEvaluation.csproj.vs10x
TestOnTheFlyConfig.csproj.vs10x
Enhanced String Handling II.pdf
using System;
using System.Text.RegularExpressions;
using EnhancedStringEvaluate;


namespace TestEvaluation.ProcessEvaluate
{
	/// <summary>
	/// Processes a construct like:
	/// 	{Date::yyyy.MM.dd}
	/// 	{Date::yy.M.d}
	/// 	Other date combinations involving:
	/// 		year: yyyy or yy
	/// 		Month: MM or M
	/// 		Day: dd or d
	/// 
	/// There is no good reason for the format to be provided in a case 
	/// sensitive manner.  If you wish to process the format in a case
	/// insensitive you may care to use regular expression replace in
	/// the DateReplace() method.
	/// 
	/// Notice that this processing has been supplanted by ProcessCurrentTime.
	/// Nevertheless, it is instructional to see how this is done in a simple
	/// case.
	/// </summary>
	public sealed class ProcessDate : IProcessEvaluate
	{
		private readonly Regex _reDate;
		private IDelimitersAndSeparator _delim;

		/// <summary> </summary>
		public ProcessDate() : this(DelimitersAndSeparator.DefaultDelimitedString) {}
		public ProcessDate(IDelimitersAndSeparator delim)
		{
			_delim = delim;
			string pattern = string.Format(@"({0})\s*Date\s*::(?<Format>([^{0}{1}])*?)({1})",
				_delim.OpenDelimEquivalent, _delim.CloseDelimEquivalent);
			RegexOptions reo = RegexOptions.Singleline | RegexOptions.IgnoreCase;
			_reDate = new Regex(pattern, reo);
		}

		#region IProcessEvaluate Members

		public void Evaluate(object src, EnhancedStringEventArgs ea)
		{
			// Initialize return code
			ea.IsHandled = false;

			// Evaluate Task 1
			string text = ea.EhancedPairElem.Value;
			if (string.IsNullOrWhiteSpace(text)) return;

			bool rc = _reDate.IsMatch(text);
			if (!rc) return;

			string replacement = _reDate.Replace(text, DateReplace);
			if (replacement == text) return;

			// Evaluate Task 2: Announce that expression was successfully handled
			ea.IsHandled = true;

			// Evaluate Task 3: Keep new value
			ea.EhancedPairElem.Value = replacement;
			return;
		}

		#endregion

		private string DateReplace(Match m)
		{
			DateTime today = DateTime.Today;
			string txt = m.Groups["Format"].Value;
			txt = txt.Replace("yyyy", today.ToString("yyyy"));
			txt = txt.Replace("yy", today.ToString("yy"));
			txt = txt.Replace("MM", today.ToString("MM"));	// 2 digit month
			txt = txt.Replace("M", today.ToString("%M"));	// 1 or 2 digit month
			txt = txt.Replace("dd", today.ToString("dd"));	// 2 digit day
			txt = txt.Replace("d", today.ToString("%d"));	// 1 or 2 digit day
			return txt;
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Avi Farah

United States United States
avifarah@gmail.com

| Advertise | Privacy | Mobile
Web04 | 2.8.140916.1 | Last Updated 16 Dec 2010
Article Copyright 2010 by Avi Farah
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid