Regular Expression-text rule substitution

Source: Internet
Author: User

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/>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.