From a post:
Http://topic.csdn.net/u/20100719/21/25879eed-7555-4ce6-b6ec-e00271838b5a.html? Seed = 505778035 & R = 67090922 # r_67090922
In fact, similar things have been written before. Once again, they will be written again, avoiding re-writing the Car Wheel and posting it on the blog.
// Created by wuyazhe @ 2010-7-20 <br/> // retain the author's statement when referencing </P> <p> using system; <br/> using system. collections. generic; <br/> using system. text; <br/> using system. text. regularexpressions; </P> <p> namespace csharpconsole02 <br/>{< br/> class Program <br/>{< br/> static void main (string [] ARGs) <br/>{< br/> // construct a rule dictionary <br/> dictionary <string, string> rule = new dictionary <string, string> (); <br/> rule. add ("ls", "connection successful"); <br/> rule. add ("da", "date"); <br/> rule. add ("ti", "Time"); <br/> rule. add ("Gi", "Stay"); <br/> rule. add ("RN", "room number"); <br/> rule. add ("G #", "guest no."); <br/> rule. add ("gn", "name"); <br/> rule. add ("Gl", "language"); <br/> rule. add ("GV", "VIP member"); <br/> rule. add ("go", "check out"); <br/> rule. add ("gS", "share"); <br/> rule. add ("GSN", "do not share "); <br/> // construct a test array <br/> string [] lines = new string [] {<br/> "ls | da100719 | ti161039 | ", <br/> "GI | rn3017 | G #17277 | Gn (en) Mr Messi | glea | gv1 | ", <br/> "Go | rn1007 | G #17277 | GSN |" };< br/> // Replace the display in sequence <br/> foreach (string line in lines) <br/> {<br/> console. writeline (multireplace (line, rule); <br/>}< br/> console. readkey (); <br/>}</P> <p> // <summary> <br/> // rule replacement method <br/> /// </Summary> <br/> // <Param name = "S"> text to be replaced </param> <br/> // <Param name = "rule"> rule dictionary </param> <br/> // <returns> text after replacement </returns> <br/> Public static string multireplace (string s, dictionary <string, string> rule) <br/>{< br/> // create rule text <br/> stringbuilder regexbuilder = new stringbuilder (); <br/> foreach (string key in rule. keys) regexbuilder. append (RegEx. escape (key) + "|"); <br/> regexbuilder. remove (regexbuilder. length-1, 1); <br/> return RegEx. replace (S, regexbuilder. tostring (), delegate (Match m) {return rule [M. value] ;}); <br/>}< br/>