dotnet Use regex-samples

Source: Internet
Author: User
Tags dotnet select from where

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

You know CSV writer would take care of the double quote and comma and new line,

So if the words have comma or new line, CSV writer would use the default text qualifier double quote

Enclose them, sample:

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

And it'll replace every double quote with 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 does this, the following are the simple sample.

One is the Remove text qualifier, another one is replace demo.

Please enjoy.

usingSystem;usingSystem.Text.RegularExpressions;usingSystem.IO;usingSystem.Collections;  Public classtest{ Public Static stringRemovetextqualifier (stringtext) {stringPattern ="^\ "(? <word>.*) \" $"; Regex RGX=NewRegex (pattern); MatchCollection matches=RGX. Matches (text);if(matches. Count>0)returnmatches[0]. groups[0]. Value.replace ("\"\"","\"");Elsereturntext;}  Public Static voidRegexreplacedemo (inttype) {stringWords ="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 t 4.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)returnm.value[0]. ToString (). ToUpper () + m.value.substring (1);Else{stringKeywordslist ="select from where and or joins left right";if(Keywordslist. IndexOf (" "+ M.value +" ") >-1)returnM.value.toupper ();ElsereturnM.value;}));}  Public Static voidMain (string[] args) {Regexreplacedemo (1); Regexreplacedemo (2);} }

dotnet Use regex-samples

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.