AI 大模型与智能应用
实践与技术

专注分享 AI 大模型、Agent 开发、深度学习等技术实战与架构设计;兼具 C#/.NET 后端开发经验

119
技术文章
8
文章分类
542
技术标签
30355
累计字数
正则表达式在C#中的使用
后端开发
2019-08-06 • 0k

正则表达式在C#中的使用

正则表达式简介 正则表达式是对字符串的操作 正则表达式是一个用来描述字符串特征的表达式。 特征:必须出现的内容、可能出现的内容、不能出现的内容。 观察字符串规律,根据规律总结特征,然后根据特定字符串的特征来编写正则表达式。 常用函数: Regex.IsMatch(); //判断是否匹配 Regex.Match();//提取某个(一个)匹配 Regex.Matches();//提取所有的匹配 Regex.Replace();//替换 Regex.Split();//分割 Regex.IsMatch() 练习1:判断是否是合法的邮政编码(6位数字) lRegex.IsMatch("100830","^[0-9]{6}$") Regex.IsMatch("119", @"^\d{6}$"); 解释:由元字符定义得知“[0-9]”表示0到9的任意字符,“{6}”表示前面的字符匹配6次,因此“[0-9]{6}”中的{6}表示对数字匹配6次(000000123)。简写表达式得知“[0-9]”可以被“\d”代替,所以第二种写法“\d{6}”也是正确的。 练习2:判断一个字符串是不是身份证号码。 长度为15位或者18位的字符串,首位不能是0。 如果是15位,则全部是数字。 如果是18位,则前17位都是数字,末位可能是数字也可能是X。 写法一:^[1-9][0-9]{14}([0-9]{2}[0-9Xx])?$ 写法二: ^([1-9][0-9]{14}|[1-9][0-9]{16}[0-9X])$ 练习3:判断字符串是否为正确的国内电话号码,不考虑分机。 •010-8888888或010-88888880或010xxxxxxx •0335-8888888或0335-88888888(区号-电话号)03358888888 •10086、10010、95595、95599、95588(5位) •13888888888(11位都是数字) Regex.IsMatch(phoneNumber, @“^((\d{3,4}\-?\d{7,8})|(\d{5})|(\d{11}))$”); //或者 Regex.IsMatch(phoneNumber, @“^((\d{3,4}\-?\d{7,8})|(\d{5}))$”); 按照要求一个一个写,都用|连起来。注意:由于区号有时为010-xxxxxxx有时为010xxxxxxx,-可有可无,所以需要?,由于-表示一个区间,所以这里要转义-。最后不要忘记在所有|的最外层加一对() Regex.Match() / Matches() string msg = "大家好呀,hello,2010年10月10日是个好日子。恩,9494.吼吼!886."; Match match1 = Regex.Match(msg, @"\d", RegexOptions.ECMAScript); //提取得到 2 (逐个提取) Match match2 = Regex.Match(msg, @"\d+", RegexOptions.ECMAScript); //提取得到2010 //提取所有的匹配的字符串 MatchCollection matches = Regex.Matches(msg, @"\d+", RegexOptions.ECMAScript); for (int i = 0; i < matches.Count; i++) { Console.WriteLine(matches[i].Value); } Console.ReadLine(); 打印结果:

dotnet 正则 C#
阅读更多
yum的基本使用以及替换国内源
运维与DevOps
2019-05-16 • 0k

yum的基本使用以及替换国内源

yum源管理 1.备份原有的yum源,以防出错时还可以还原 mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup 2.下载同时改名 wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo #其他源 wget http://mirrors.163.com/.help/CentOS7-Base-163.repo #centos7系统的 wget http://mirrors.163.com/.help/CentOS6-Base-163.repo #centos6系统的 wget http://mirrors.163.com/.help/CentOS5-Base-163.repo #centos5系统的 (注:如果下载出现错误,可能是变更了下载的地址,可以到:http://mirrors.163.com/.help/centos.html 找新的下载地址。没有安装wget的,要先安装。) 3.产生新的缓存。 yum clean all yum makecache 至此源更新完成,可以 yum -y update 测试一下更新的速度。 安装软件 以安装Docker为例 安装最新版 yum install -y docker-ce docker-ce-cli containerd.io 安装指定版本 列出可用版本 $ yum list docker-ce --showduplicates | sort -r docker-ce.x86_64 3:18.09.1-3.el7 docker-ce-stable docker-ce.x86_64 3:18.09.0-3.el7 docker-ce-stable docker-ce.x86_64 18.06.1.ce-3.el7 docker-ce-stable docker-ce.x86_64 18.06.0.ce-3.el7 docker-ce-stable 安装指定版本 <VERSION_STRING>需要替换为第二列的版本号,如:18.06.0.ce-3.el7 $ sudo yum install docker-ce-<VERSION_STRING> docker-ce-cli-<VERSION_STRING> containerd.io 其他 源目录:

linux yum 包管理
阅读更多
Net Core 3.x 禁用序列化时自动首字母小写
后端开发
2019-03-11 • 0k

Net Core 3.x 禁用序列化时自动首字母小写

public void ConfigureServices(IServiceCollection services) { services.AddControllers().AddJsonOptions(config => { //去掉转小写功能 // System.Text.Json.JsonNamingPolicy.CamelCase:转小写 // null:不转小写 config.JsonSerializerOptions.PropertyNamingPolicy =null; }); }

dotnet .NET Core 序列化
阅读更多
NuGet源中国加速镜像,博客园,华为等
后端开发
2019-03-11 • 0k

NuGet源中国加速镜像,博客园,华为等

打开Visual Studio => 工具 => NuGet包管理器 => 程序包管理器设置 NuGet微软官方中国镜像地址: https://nuget.cdn.azure.cn/v3/index.json 官方默认源: https://api.nuget.org/v3/index.json 博客园: https://nuget.cnblogs.com/v3/index.json 华为: https://repo.huaweicloud.com/repository/nuget/v3/index.json MyNuGet: https://dotnet.myget.org/F/dotnet-core/api/v3/index.json 这是一个很激进的 NuGet 源,包含各种日构建包(其中包括 .NET Standard 或者 .NET Core 等库的日构建版本),所以如果你希望尝试最新的 API 最新的功能,最好设置此 NuGet 源。 早期版本: https://www.nuget.org/api/v2/ https://nuget.org/api/v2/

dotnet NuGet 包管理
阅读更多
T4模板 生成多个文件 映射整个库表
后端开发
2019-03-11 • 0k

T4模板 生成多个文件 映射整个库表

<#@ output extension="txt" #> <#@ template debug="false" hostspecific="true" language="C#" #> <#@ assembly name="System.Data" #> <#@ assembly name="System.xml" #> <#@ import namespace="System.Collections.Generic" #> <#@ import namespace="System.Data.SqlClient" #> <#@ import namespace="System.Data" #> <#@ assembly name="System.Core" #> <#@ import namespace="System.Linq" #> <#@ include file="MultiOutput.tt" #> <# string connectionString= "server=.;database=57blogDB;uid=sa;pwd=123;"; SqlConnection conn = new SqlConnection(connectionString); conn.Open(); string selectQuery ="SET FMTONLY ON; select * from @tableName; SET FMTONLY OFF;"; SqlCommand command = new SqlCommand(selectQuery,conn); SqlDataAdapter ad = new SqlDataAdapter(command); System.Data.DataSet ds = new DataSet(); System.Data.DataTable schema = conn.GetSchema("Tables"); foreach(System.Data.DataRow row in schema.Rows) { ds.Tables.Clear(); string tb_name = row["TABLE_NAME"].ToString(); command.CommandText = selectQuery.Replace("@tableName",row["TABLE_NAME"].ToString()); ad.FillSchema(ds,SchemaType.Mapped,tb_name); string content=tb_name.ToString(); // manager.StartNewFile(tb_name+".cs"); #> using System.Collections.Generic; using System.Linq; using System.Text; using System.Text.RegularExpressions; using System.Data; using System.Data.OleDb; using System.Configuration; using System.Data.SqlClient; using System; namespace MyProject.Entities { public class <#= content#>Model { //------------------------------------------- <# string selectQueryt = "select * from "+content; string tableName=content; System.Data.DataTable schemat = null; SqlCommand selectcommandt = new SqlCommand(selectQueryt, conn); SqlDataAdapter sdat = new SqlDataAdapter(selectcommandt); System.Data.DataSet dsst = new System.Data.DataSet(); sdat.Fill(dsst); schemat=dsst.Tables[0]; System.Data.DataTable dt = dsst.Tables[0]; foreach (DataColumn dc in dsst.Tables[0].Columns) {#> private <#= dc.DataType.Name #> _<#= dc.ColumnName.Replace(dc.ColumnName[0].ToString(), dc.ColumnName[0].ToString().ToLower()) #>; /// <summary> /// <#= dc.ColumnName #> /// </summary> public <#= dc.DataType.Name #> <#= dc.ColumnName #> { get { return _<#= dc.ColumnName.Replace(dc.ColumnName[0].ToString(), dc.ColumnName[0].ToString().ToLower()) #>; } set { _<#= dc.ColumnName.Replace(dc.ColumnName[0].ToString(), dc.ColumnName[0].ToString().ToLower()) #> = value; } } <#}#> //------------------------------------------------- } } <# SaveOutput(""+row["TABLE_NAME"].ToString()+".cs"); #> <# } DeleteOldOutputs(); #>

dotnet T4模板 代码生成
阅读更多
把C#程序(含多个Dll)合并成一个exe的超简单方法
后端开发
2019-03-11 • 0k

把C#程序(含多个Dll)合并成一个exe的超简单方法

开发程序的时候经常会引用一些第三方的DLL,然后编译生成的exe文件就不能脱离这些DLL独立运行了。 但是,很多时候我们本想开发一款只需要一个exe就能完美运行的小工具。那该怎么办呢? 下文介绍一种超简单的方法,不用写一行代码就可轻松实现。 这里我们需要用到一款名为Fody.Costura的工具。Fody.Costura是一个Fody框架下的插件,可通过Nuget安装到VS工程中。安装之后,就可以将项目所依赖的DLL(甚至PDB)文件全部打包到EXE文件里。 使用方法 在VS中,通过Nuget为目标EXE工程安装Costura.Fody。 重新构建项目。 构建完成后,到项目的输出目录下找到新生成的EXE文件,你同时会发现输出目录下仍然存在那些DLL。不过不用担心,这个EXE已经能够独立运行了。你可以把这些DLL全部删除后再运行EXE试试。 另外,Fody.Costura还支持一些进阶的特性,例如: 临时程序集文件:在运行EXE前自动,自动将DLL从EXE中解压到文件夹系统中,再通过常规的方式加载该DLL。 合并非托管的DLL:Fody.Costura可以合并非托管的DLL,但是不会自动合。如果你的程序涉及非托管DLL,那么你需要通过修改Fody.Costura的配置文件来显示地告诉它你想合并哪些非托管的DLL。 预加载DLL:Fody.Costura可以帮助你在程序启动时预先加载某些DLL,你甚至可以指定这些DLL的加载顺序。 以上这些进阶特性都需要你通过修改Fody.Costura的配置文件来实现,具体的操作步骤可以参考它的官方文档。 好了,Fody.Costura的使用方式已经介绍完了。如果你对Fody.Costura的实现原理感到好奇,可以接着往下看。 实现原理介绍 当CLR试图加载一个程序集但加载失败时,它会引发AppDomain.AssemblyResolve事件。我们的程序可以监听这个事件,并且在这个事件的处理函数中返回这个CLR试图加载的程序集,从而使程序得以继续正常运行。 Fody.Costura在构建项目时会把EXE引用到的DLL全部嵌入到EXE文件中。当程序在运行的过程中用到其中某个DLL的时候(此时由于CLR无法找到该DLL文件,导致AppDomain.AssemblyResolve事件被触发)再从EXE文件的嵌入资源中提取所需的DLL。 下面这两个函数就是Fody.Costura实现这部分逻辑的代码。 public static void Attach() { var currentDomain = AppDomain.CurrentDomain; currentDomain.AssemblyResolve += (s, e) => ResolveAssembly(e.Name); } public static Assembly ResolveAssembly(string assemblyName) { if (nullCache.ContainsKey(assemblyName)) { return null; } var requestedAssemblyName = new AssemblyName(assemblyName); var assembly = Common.ReadExistingAssembly(requestedAssemblyName); if (assembly != null) { return assembly; } Common.Log("Loading assembly '{0}' into the AppDomain", requestedAssemblyName); assembly = Common.ReadFromEmbeddedResources(assemblyNames, symbolNames, requestedAssemblyName); if (assembly == null) { nullCache.Add(assemblyName, true); // Handles retargeted assemblies like PCL if (requestedAssemblyName.Flags == AssemblyNameFlags.Retargetable) { assembly = Assembly.Load(requestedAssemblyName); } } return assembly; } 可以看到,Attach方法监听了AppDomain.AssemblyResolve事件。当CLR无法成功加载某个程序集时, AssemblyResolve事件处理函数会被执行。AssemblyResolve会尝试通过Common.ReadFromEmbeddedResources方法从已加载的程序集的嵌入资源中获取目标程序集,并返回给CLR。 看到这里,你可能会问,Attach方法是在什么时候执行的呢? 其实是这样的,对于C#语言来说,CLR隐藏了一个大招——CLR可以在每个模块(每个程序集都含有一个或多个模块)加载之前执行一些初始化的代码。但是很遗憾,C#语言无法控制这部分代码。Fody.Costura则是在内部将IL代码直接注入到EXE程序集内部模块的初始化函数中,而这部分IL代码其实就是执行了Attach方法。这样一来,EXE程序集被加载后,Attach方法就能够立即得到调用了。 以上就是Fody.Costura实现原理的简单介绍。 问题解决: 如果安装失败 替换一下源地址

dotnet C# Fody.Costura
阅读更多
F5负载均衡架构图
运维与DevOps
2018-08-06 • 0k

F5负载均衡架构图

鸣谢: https://blog.csdn.net/qq_35611533/article/details/51917279?locationNum=1&fps=1

负载均衡 F5 高可用架构
阅读更多
DotNet Core AutoMapper实体映射学习记录
后端开发
2018-03-11 • 0k

DotNet Core AutoMapper实体映射学习记录

AutoMapper:实体间的映射(数据库Model转Dto) 1、创建Model using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; namespace AutoMapperDemo { public class Entity { } public class PostModel { public Guid Id { get; set; } public long SerialNo { get; set; } public string Title { get; set; } public string Author { get; set; } public string Image { get; set; } public short CategoryCode { get; set; } public bool IsDraft { get; set; } public string Content { get; set; } public DateTime ReleaseDate { get; set; } public virtual IList<CommentModel> Comments { get; set; } } public class CommentModel { public Guid Id { get; set; } public string Email { get; set; } public string Content { get; set; } public DateTime CommentDate { get; set; } } } 2、创建Dto using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; namespace AutoMapperDemo { public class ViewModel { } public class PostViewModel { public Guid Id { get; set; } public long SerialNo { get; set; } public string Title { get; set; } public string Author { get; set; } public short CategoryCode { get; set; } public string Category => CategoryCode == 1001 ? ".NET" : "杂谈"; public string ReleaseDate { get; set; } public short CommentCounts { get; set; } public virtual int Count { get; set; } } } 3、创建映射规则 using AutoMapper; using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Threading.Tasks; namespace AutoMapperDemo { public class PostProfile : Profile { /// <summary> /// ctor /// </summary> public PostProfile() { // 配置 mapping 规则 // CreateMap<PostModel, PostViewModel>() .ForMember(destination => destination.CommentCounts, source => source.MapFrom(i => i.Comments.Count())) .ForMember(destination => destination.ReleaseDate, source => source.ConvertUsing(new DateTimeConverter())); } } public class DateTimeConverter : IValueConverter<DateTime, string> { public string Convert(DateTime source, ResolutionContext context) => source.ToString("yyyy-MM-dd HH:mm:ss"); } } 4、创建mapper扩展 映射规则注入

dotnet AutoMapper 实体映射
阅读更多
sitemap和sitemapindex
工具与效率
2017-05-17 • 0k

sitemap和sitemapindex

介绍: https://support.google.com/webmasters/answer/75712?hl=zh-Hans 实例: http://www.thepaper.cn/sitemap.xml <sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> <sitemap> <loc>http://www.thepaper.cn/sitemap-1.xml</loc> <lastmod>2017-10-13</lastmod> </sitemap> <sitemap> <loc>http://www.thepaper.cn/asktopic.xml</loc> <lastmod>2017-10-13</lastmod> </sitemap> <sitemap> <loc>http://www.thepaper.cn/newsDetail-7.xml</loc> <lastmod>2017-10-13</lastmod> </sitemap> <sitemap> <loc>http://www.thepaper.cn/newsDetail-6.xml</loc> <lastmod>2017-10-13</lastmod> </sitemap> <sitemap> <loc>http://www.thepaper.cn/newsDetail-5.xml</loc> <lastmod>2017-10-13</lastmod> </sitemap> <sitemap> <loc>http://www.thepaper.cn/newsDetail-4.xml</loc> <lastmod>2017-10-13</lastmod> </sitemap> <sitemap> <loc>http://www.thepaper.cn/newsDetail-3.xml</loc> <lastmod>2017-10-13</lastmod> </sitemap> <sitemap> <loc>http://www.thepaper.cn/newsDetail-2.xml</loc> <lastmod>2017-10-13</lastmod> </sitemap> <sitemap> <loc>http://www.thepaper.cn/newsDetail-1.xml</loc> <lastmod>2017-10-13</lastmod> </sitemap> </sitemapindex>

SEO sitemap sitemapindex
阅读更多