dotnet use regex two samples
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>)
return matches[].Groups[].Value.Replace("\"\"", "\"");
else
return text;
}
public static void regexReplaceDemo(int type)
{
string words = "letter alphabetical missing lack release " +
"penchant slack acryllic laundry cease";
if(type == )
words = @"select *from table1 left join table2 on table1.col1=table2.col2
right join t3 on table1.col2=t3.col1 full join t4 on t4.c1=t3.col2 where 1=1
and 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 capitalize
if(type == )
return m.Value[].ToString().ToUpper() + m.Value.Substring();
else
{
string keywordslist = " select from where and or join left right full on ";
if(keywordslist.IndexOf(" " + m.Value + " ")>-)
return m.Value.ToUpper();
else
return m.Value;
}
}));
}
public static void Main(string[] args)
{
regexReplaceDemo();
regexReplaceDemo();
}
}
dotnet use regex two samples的更多相关文章
- 【ASP.NET Core分布式项目实战】(五)Docker制作dotnet core控制台程序镜像
Docker制作dotnet core控制台程序镜像 基于dotnet SDK 新建控制台程序 mkdir /home/console cd /home/console dotnet new cons ...
- dotnet core 使用 CoreRT 将程序编译为 Native 程序
现在微软有一个开源项目 CoreRT 能通过将托管的 .NET Core 编译为单个无依赖的 Native 程序 这个项目现在还没发布,但是能尝试使用,可以带来很多的性能提升 使用 CoreRT 发布 ...
- .NET 2.0 参考源码索引
http://www.projky.com/dotnet/2.0/Microsoft/CSharp/csharpcodeprovider.cs.htmlhttp://www.projky.com/do ...
- .NET Core系列 :4 测试
2016.6.27 微软已经正式发布了.NET Core 1.0 RTM,但是工具链还是预览版,同样的大量的开源测试库也都是至少发布了Alpha测试版支持.NET Core, 这篇文章 The Sta ...
- .NET Core 2.0 应用程序大小减少50%
.NET Core 2.0减小体积瘦身官方工具 IL Linker. IL Linker 来源于mono的linker https://github.com/mono/linker,目前还是预览版本 ...
- .NET Core单文件发布静态编译AOT CoreRT
.NET Core单文件发布静态编译AOT CoreRT,将.NET Core应用打包成一个可执行文件并包含运行时. 支持Windows, MacOS and Linux x64 w/ RyuJIT ...
- ASP.NET Core在Azure Kubernetes Service中的部署和管理
目录 ASP.NET Core在Azure Kubernetes Service中的部署和管理 目标 准备工作 注册 Azure 账户 AKS文档 进入Azure门户(控制台) 安装 Azure Cl ...
- 树莓派3B+上运行.Net Core项目
最近买了个树莓派3B+,准备把自己写的一个.Net Core爬虫挂在上面跑 买之前没有想到树莓派因为是今年新出的,导致驱动以及系统啥的都不是很完善,导致走了很多弯路,早知道买树莓派3就不用那么多折腾了 ...
- .NET Core 2.0应用程序大小减少50%
.NET Core 2.0应用程序减小体积瘦身官方工具 IL Linker. IL Linker 来源于mono的linker https://github.com/mono/linker,目前还是 ...
随机推荐
- .NET开发 正则表达式中的 Bug
又发现了一个 .net 的 bug!最近在使用正则表达式的时候发现:在忽略大小写的时候,匹配值从 0xff 到 0xffff 之间的所有字符,正则表达式竟然也能匹配两个 ASCII 字符:i(code ...
- Microsoft Excel软件打开文件出现文件的格式与文件扩展名指定格式不一致?
今天winform代码做一个datagridview数据导出功能,导出的excel文件的后缀是*.xls(Micorsoft Excel 2000), 而本机新建的excel文件的后缀是 *.xlsx ...
- 在Android设备上判断设备是否支持摄像头
private boolean hasCamera(){ boolean hasCamera=false; PackageManager pm=getActivity().getPackageMana ...
- poolboy的坑
poolboy是Erlang中运用非常广泛的进程池库,它有很多优点,使用简单,在很多项目中都能看到它的身影.不过,它也有一些坑,使用时候需要注意.(本文对poolboy的分析基于1.5.1版本) wo ...
- [小北De编程手记] : Lesson 06 - Selenium For C# 之 流程控制
无论你是用哪一种自动化测试的驱动框架,当我们构建一个复杂应用程序的自动化测试的时候.都希望构建一个测试流程稳定,维护成本较低的自动化测试.但是,现实往往没有理想丰满.而这一篇,我会为大家讲解我们在使用 ...
- 功放AUX接口解析
功放上的AUX输入端口就是音频输入端口,用来连接音频输出设备:PC机上声卡.MP3播放器.CD机等
- SharpGL学习笔记(十七) 立体文字和平面文字
在写有关文字的主题前,笔者翻阅了几本书上的相关章节,研究了几天无果. 徐明亮<OpenGL游戏编程>书中介绍的是“位图字体”,也就是把字体栅格化,然后画出来.照着书上的VC代码翻译为C#的 ...
- 向java的main()传入大量参数
项目中有一些用java写成的可执行的工具,需要调用者传入大量的参数.最开始,我使用的是最传统的方式,直接一个传入参数数组,于是有如下这么壮观的代码: public static void main(S ...
- SharePoint 2013 排错之"Code blocks are not allowed in this file"
今天,设置页面布局的自定义母版页时,设置完了以后保存,然后预览报错,错误如下截图:删掉自定义母版页的MasterPageFile属性,页面依然报错:感觉甚是奇怪,因为有版本控制,还原为最初的版本,依然 ...
- Windows 下Apace tomcat
java JDK安装: 1. 官方www.oracle.com 下载jdk 2. 环境变量配置 (1)新建->变量名:JAVA_HOME变量值:C:\Program Files (x86)\Ja ...