C # regular expressions match the disk path and file name

Source: Internet
Author: User
Regular Expression related knowledge, refer to the msdn documentation: http://msdn.microsoft.com/zh-cn/library/az24scfc.aspx and http://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 ();
}
}

The following is a simple arrangement to facilitate future calls (multiple extension names are supported ):

Using System;
Using System. Collections. Generic;
Using System. LINQ;
Using System. text;
Using System. collections;
Using System. reflection;
Using System. Text. regularexpressions;

Namespace Leleapplication1
{
Class Program
{
///   <Summary>
/// Path Classification
///   </Summary>
Public Enum Routetype
{
///   <Summary>
/// File Name Only
///   </Summary>
Nameonly = 0 ,
///   <Summary>
/// File name (including suffix)
///   </Summary>
Filename = 1 ,
///   <Summary>
/// Only suffix (including .)
///   </Summary>
Filesuffix = 2 ,
///   <Summary>
/// Path except file name
///   </Summary>
Pathpolictname = 3 ,
///   <Summary>
/// Path except suffix
///   </Summary>
Pathpattern tsuffix = 4
}
///   <Summary>
///  ProgramEntry Function
///   </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. pathtrotname ));
Console. writeline (getpartroute (strpath, routetype. pathexceptsuffix ));

Console. Readline ();
}
///   <Summary>
/// Obtain the file path using regular expressions
///   </Summary>
///   <Param name = "strroute"> Complete file name </Param>
///   <Param name = "Rtype"> To obtain the path type </Param>
///   <Returns> </returns>
Public Static String Getpartroute ( String Strroute, routetype Rtype)
{
String Strresult = String . Empty;

RegEx =NewRegEx (@"^ (? <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. pathpolictname:
Strresult = result. Result ( " $ {Fpath} " ); Break ;
Case Routetype. pathpolictsuffix:
Strresult = result. Result ( " $ {Fpath} " ) + Result. Result ( " $ {Fname} " ) + Result. Result ( " $ {Namext} " ); Break ;
Default :
Strresult = strroute; Break ;
}
}

ReturnStrresult;
}
}
}

Click here to download

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.