文章目录
- [1 要求](#1 要求)
- [2 分析 与 实现](#2 分析 与 实现)
1 要求
写一个函数,获取路径下的文件名(不包含路径和扩展名),并分离出文件名fileName
,文件名编号SN
,文件名前缀WMT
;
输入文件路径,解析出不带"."
后缀的文件名fileName,然后fileName进一步拆分为SN+WMF 格式,其中WMT是MatchFilter数组中去掉"."后缀的部分,那么 fileName 减去末尾的WMF就得到SN。
E:\MTF\A1\StandardData\00000000000001Tele.csv
拆分结果 fileName = 00000000000001Tele
,SN = 00000000000001
,WMT =Tele
;
2 分析 与 实现
文件名由连续的数字编号+连续的字母编号组成,且顺序是数字标号在前,英文字母编号在后;
cpp
// 从文件路径中提取文件名并分割为数字编号和英文后缀
private void ExtractFileNameParts2(string filePath, ref string fileName,ref string SN, ref string WMT)
{
fileName = Path.GetFileNameWithoutExtension(filePath); // 获取不带扩展名的文件名
Regex regex = new Regex(@"(\d+)([a-zA-Z]+)$"); // 匹配文件名中的数字编号和英文后缀
Match match = regex.Match(fileName);
if (match.Success)
{
SN = match.Groups[1].Value; // 数字编号部分
WMT = match.Groups[2].Value; // 英文后缀部分
}
else
{
SN = string.Empty;
WMT = string.Empty;
}
}