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