欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

C#正则表达式 匹配磁盘路径和文件名

程序员文章站 2022-03-04 13:07:27
...

正则表达式相关知识,参考MSDN文档:http://msdn.microsoft.com/zh-cn/library/az24scfc.aspxhttp://msdn.microsoft.com/zh-cn/library/63ew9az0.aspx

class Program
{
    static void Main(string[] args)
    {
        Regex reg = new Regex(@"^(?<fpath>([a-zA-Z]:\\)([\s\.\-\w]+\\)*)(?<fname>[\w]+.[\w]+)");

        Match result = reg.Match(@"D:\Program Files\AliWangWang\7.10.07C\emotions\TaoDoll\Sniffer.dat");

        if (result.Success)
        {
            Console.WriteLine("[Full]:" + result.Value);

            Console.WriteLine("[Part1]:" + result.Result("${fpath}"));
            Console.WriteLine("[Part2]:" + result.Result("${fname}"));
        }

        Console.ReadLine();
    }
} 

简单整理如下,方便以后调用(支持多后缀名):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Reflection;
using System.Text.RegularExpressions;

namespace ConsoleApplication1
{
    class Program
    {
        /// <summary>
        /// 路径分类
        /// </summary>
        public enum RouteType
        {
            /// <summary>
            /// 仅文件名
            /// </summary>
            NameOnly = 0,
            /// <summary>
            /// 文件名(包括后缀)
            /// </summary>
            FileName = 1,
            /// <summary>
            /// 仅后缀名(包括.)
            /// </summary>
            FileSuffix = 2,
            /// <summary>
            /// 除文件名之外的路径
            /// </summary>
            PathExceptName = 3,
            /// <summary>
            /// 除后缀之外的路径
            /// </summary>
            PathExceptSuffix = 4
        }
        /// <summary>
        /// 程序入口函数
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            string strPath = @"D:\Program Files\Advanced PDF Password Recovery\Sniffer.dat.doc.bak.zip";

            Console.WriteLine(GetPartRoute(strPath, RouteType.NameOnly));
            Console.WriteLine(GetPartRoute(strPath, RouteType.FileName));
            Console.WriteLine(GetPartRoute(strPath, RouteType.FileSuffix));
            Console.WriteLine(GetPartRoute(strPath, RouteType.PathExceptName));
            Console.WriteLine(GetPartRoute(strPath, RouteType.PathExceptSuffix));

            Console.ReadLine();
        }
        /// <summary>
        /// 正则获取文件路径
        /// </summary>
        /// <param name="strRoute">完整文件名</param>
        /// <param name="rType">要获取路径类型</param>
        /// <returns></returns>
        public static string GetPartRoute(string strRoute, RouteType rType)
        {
            string strResult = string.Empty;

            Regex regex = new Regex(@"^(?<fpath>([a-zA-Z]:\\)([\s\.\-\w]+\\)*)(?<fname>[\w]+)(?<namext>(\.[\w]+)*)(?<suffix>\.[\w]+)");

            Match result = regex.Match(strRoute);

            if (result.Success)
            {
                switch (rType)
                {
                    case RouteType.NameOnly:
                        strResult = result.Result("${fname}") + result.Result("${namext}");break;
                    case RouteType.FileName:
                        strResult = result.Result("${fname}") + result.Result("${namext}") + result.Result("${suffix}"); break;
                    case RouteType.FileSuffix:
                        strResult = result.Result("${suffix}"); break;
                    case RouteType.PathExceptName:
                        strResult = result.Result("${fpath}"); break;
                    case RouteType.PathExceptSuffix:
                        strResult = result.Result("${fpath}") + result.Result("${fname}") + result.Result("${namext}"); break;
                    default:
                        strResult = strRoute; break;
                }
            }

            return strResult;
        }
    }
}
相关标签: 正则表达式 C#