dotnet use regex two samples

來源:互聯網
上載者:User

標籤:style   blog   io   ar   color   os   sp   on   div   

   

One sample is used to replace double quote from words which encapsulated by csvwriter ,

you know csv writer will take care of the double quote and comma and new line,

So if the words has comma or new line, csv writer will use default text qualifier double quote

Enclose them, sample :

Source : evan,yao CSV :"evan,yao"

And it will replace every double quote with two double quote.

Such as source data is evan,yao" csv: "evan,yao"""

So when we read csv words, we should replace the additional double quote,

I use c# and regex to do this, the following is two simple sample.

One is remove text qualifier, another one is replace demo.

Please enjoy.

using System;using System.Text.RegularExpressions;using System.IO;using System.Collections;   public class test{public static string removeTextQualifier(string text){string pattern = "^\"(?<word>.*)\"$";Regex rgx = new Regex(pattern);MatchCollection matches = rgx.Matches(text);if(matches.Count>0)return matches[0].Groups[0].Value.Replace("\"\"", "\"");elsereturn text;}   public static void regexReplaceDemo(int type){string words = "letter alphabetical missing lack release " +"penchant slack acryllic laundry cease";if(type == 2)words = @"select *from table1 left join table2 on table1.col1=table2.col2right join t3 on table1.col2=t3.col1 full join t4 on t4.c1=t3.col2 where 1=1and 2=2 or 3=3";Console.WriteLine("Original words:");Console.WriteLine(words);Console.WriteLine();Console.WriteLine("First Letter Capital words:");Console.WriteLine(Regex.Replace(words, @"\w+", (m) => {//change the first letter to capitalizeif(type == 1)return m.Value[0].ToString().ToUpper() + m.Value.Substring(1);else{string keywordslist = " select from where and or join left right full on ";if(keywordslist.IndexOf(" " + m.Value + " ")>-1)return m.Value.ToUpper();elsereturn m.Value;}}));}   public static void Main(string[] args){regexReplaceDemo(1);regexReplaceDemo(2);} }

 

   

 

dotnet use regex two samples

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.