Click here to Skip to main content
Click here to Skip to main content

Using letter templates

, 21 Apr 2004
Rate this:
Please Sign up or sign in to vote.
An article on using regular expression in MS Framework

Introduction

Hello. Let’s begin our conversation. They’re many cases when you have to use a text template. For example, you have a mail engine, which performs sending of letters to many users and you don’t want to create special letter for every user (let’s imagine that there are thousand). Your email must be something like that: ”Hello Mr. #username, your email #email is not valid any more. Please, contact to #admin to solve a problem”.

You see, that you have just a template and that letter can be send to any users after substitution user’s data. To perform that task using MS .NET Framework is very simple. To do that, we will use Regular Expressions.

Using the code

The input of our method of replacing (we will call it Parse) will be a string with the text. The result value is a string with transformed data. As the keys we will use notation like this: #any_text_or_numbers.

But before parsing we have to know what text should be replicated. For that goal we’ll use a hash table to store keys and their values. Before, describing the usage of regular expressions, let’s consider an algorithm of replacing keys.

//******************************** 
//Get the index of the start position to copy string 
//and it's length 
//******************************** 
copyfrom = lastindex+lastlenght; 
copycount = m.Index - (lastindex+lastlenght); 
//******************************** 
//THE RESULT STRING IS THE STRING FROM INPUT WHERE DATA 
//PUTTED ON THE PLACE OF PARAMETERS 
//******************************** 
result+=text.Substring(copyfrom, copycount )+GetHashData(m.Value); 
//******************************** 
//Remember the last index of parameter and the last length 
//******************************** 
lastindex = m.Index; 
lastlenght = m.Length; 
m = m.NextMatch(); 

Let’s back to Regular Expressions (RE). The RE which corresponds to : #any_text_or_numbers is @"#(\w)+".

Additional information on all symbols used in RE you will easily find in MSDN. Here is the code for the methods of replacing keys by their values.

//******************************** 
//Change keys by their values 
//******************************** 
public string Parse (string text) 
{ 
  string result = string.Empty; 
  string pat = @"#(\w)+"; 
  // Compile the regular expression. 
  Regex r = new Regex(pat, RegexOptions.IgnoreCase); 
  // Match the regular expression pattern against a text string. 
  Match m = r.Match(text); 
  int lastindex=0; 
  int lastlenght = 0; 
  int copyfrom=0; 
  int copycount = text.Length; 
  while (m.Success) //look throw all matches
  { 
    //******************************** 
    //Get the index of the start position to copy string 
    //and it's length 
    //******************************** 
    copyfrom = lastindex+lastlenght; 
   copycount = m.Index - (lastindex+lastlenght); 
    //******************************** 
    //THE RESULT STRING IS THE STRING FROM INPUT WHERE DATA 
    //PUTTED ON THE PLACE OF PARAMETERS 
    //******************************** 
    result+=text.Substring(copyfrom, copycount )
       +GetHashData(m.Value); 
    //******************************** 
    //Remember the last index of parameter and the last length 
    //******************************** 
    lastindex = m.Index; 
    lastlenght = m.Length; 
    m = m.NextMatch(); 
  } 
  if ((lastindex+lastlenght)<text.Length) 
  { 
    result+=text.Substring(lastindex+lastlenght, 
          text.Length-(lastindex+lastlenght) ); 
  } 
  return result; 
}

That’s it for now.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Kyrylo Yatsenko
Web Developer SOCIETE GENERALE CREDIT INVESTMENT BANKING
France France
Kyrylo has started his IT career as software developer. Shortly he became Team Lead developer leading the projects based on MS .NET Framework technology.
 
Today he is working for SOCIETE GENERALE CIB, trying to combine his experience of software developer with bank's activities on electronic markets.

Comments and Discussions

 
GeneralMy vote of 1 Pinmembermacrocephalic726-Jul-09 19:31 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.141220.1 | Last Updated 22 Apr 2004
Article Copyright 2004 by Kyrylo Yatsenko
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid