使用 C# 中的正则表达式从完整路径解析文件名
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/223162/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Parse filename from full path using regular expressions in C#
提问by Jonathan Beerhalter
How do I pull out the filename from a full path using regular expressions in C#?
如何使用 C# 中的正则表达式从完整路径中提取文件名?
Say I have the full path C:\CoolDirectory\CoolSubdirectory\CoolFile.txt
.
说我有完整的路径C:\CoolDirectory\CoolSubdirectory\CoolFile.txt
。
How do I get out CoolFile.txt using the .NET flavor of regular expressions? I'm not really good with regular expressions, and my RegEx buddy and me couldn't figure this one out.
如何使用正则表达式的 .NET 风格获取 CoolFile.txt?我对正则表达式不太擅长,我和我的 RegEx 伙伴无法弄清楚这一点。
Also, in the course of trying to solve this problem, I realized that I can just use System.IO.Path.GetFileName
, but the fact that I couldn't figure out the regular expression is just making me unhappy and it's going to bother me until I know what the answer is.
另外,在尝试解决这个问题的过程中,我意识到我可以只使用 System.IO.Path.GetFileName
,但是我无法弄清楚正则表达式的事实只会让我不高兴,并且在我知道答案之前它会困扰我是。
采纳答案by bdukes
// using System.Text.RegularExpressions;
/// <summary>
/// Regular expression built for C# on: Tue, Oct 21, 2008, 02:34:30 PM
/// Using Expresso Version: 3.0.2766, http://www.ultrapico.com
///
/// A description of the regular expression:
///
/// Any character that is NOT in this class: [\], any number of repetitions
/// End of line or string
///
///
/// </summary>
public static Regex regex = new Regex(
@"[^\]*$",
RegexOptions.IgnoreCase
| RegexOptions.CultureInvariant
| RegexOptions.IgnorePatternWhitespace
| RegexOptions.Compiled
);
UPDATE:removed beginning slash
更新:删除了开始斜杠
回答by Dour High Arch
Why must you use regular expressions? .NET has the built-in Path.GetFileName()
method specifically for this which works across platforms and filesystems.
为什么必须使用正则表达式?.NET 有Path.GetFileName()
专门为此的内置方法,可以跨平台和文件系统工作。
回答by Seth Petry-Johnson
Here's one approach:
这是一种方法:
string filename = Regex.Match(filename, @".*\([^\]+$)").Groups[1].Value;
Basically, it matches everything between the very last backslash and the end of the string. Of course, as you mentioned, using Path.GetFileName() is much easier and will handle lots of edge cases that are a pain to handle with regex.
基本上,它匹配最后一个反斜杠和字符串末尾之间的所有内容。当然,正如您所提到的,使用 Path.GetFileName() 更容易,并且可以处理许多使用正则表达式处理起来很痛苦的边缘情况。
回答by Jeff Yates
\w+:\(\w+\)*(?<file>\w*\.\w*)
This obviously would need expanding to cover all path characters, but the named group "file" contains your filename for the example path given.
这显然需要扩展以覆盖所有路径字符,但命名组“文件”包含给定示例路径的文件名。
回答by dlamblin
Shorter:
更短:
string filename = Regex.Match(fullpath, @"[^\]*$").Value;
Or:
或者:
string filename = Regex.Match(fullpath, "[^\"+System.IO.Path.PathSeparator+"]*$").Value;
Without Regex
:
没有Regex
:
string[] pathparts = fullpath.Split(new []{System.IO.Path.PathSeparator});
string file = pathparts[pathparts.Length-1];
The official library support you mentioned:
您提到的官方库支持:
string file = System.IO.Path.GetFileName(fullpath);
回答by Jonathan C Dickinson
You should rather use the System.Path class. It will mean you will have to worry about less if you ever decide to support Mono/Linux (dlamblin's example takes the path seperator into account, but you may get a strange OS that has strange paths). The System.Path class can also combine two paths into one. So for example:
您应该使用 System.Path 类。这意味着如果您决定支持 Mono/Linux,您将不必担心更少(dlamblin 的示例考虑了路径分隔符,但您可能会得到一个具有奇怪路径的奇怪操作系统)。System.Path 类还可以将两条路径合二为一。例如:
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "My App Stuff");
would resolve to:
将决定:
- Windows: C:\Documents and Settings\[User]\My Documents\My App Stuff
- Linux: /[User]/My App Stuff
- Windows:C:\Documents and Settings\[用户]\My Documents\My App Stuff
- Linux:/[用户]/我的应用程序