gpt4 book ai didi

下载电影的 C# 模式匹配

转载 作者:太空宇宙 更新时间:2023-11-03 21:15:52 24 4
gpt4 key购买 nike

您好,我正在寻找一种模式匹配,它允许我在电影下载后从文件名中获取匹配集合。文件示例是

The.Voices.2014.720p.BDRip.x264.AC3-WiNTeaM.avi
13.Sins.2014.1080p.BluRay.DTS.x264-HDS.mkv
A.Million.Ways.to.Die.in.the.West.2014.1080p.WEB-DL.x264.AC3-EVO.mkv
Ant-Man.2015.1080p.BluRay.x264-SPARKS.mkv

我希望能够提取没有句点、年份和质量的电影名称。

我认为不能保证这些按此顺序出现,因此理想情况下模式需要灵活。

在此先感谢您的帮助

最佳答案

extract the name of the movie without periods, the year and the quality.

使用前向否定展望确保年份将成为正则表达式解析的 anchor ,而不一定是电影标题的一部分。

我添加了 John Belushi 题为 1941 的电影作为测试项目。

string data =
@"1941.1981.1080p.WEB-DL.x264.AC3-EVO.mkv
The.Voices.2014.720p.BDRip.x264.AC3-WiNTeaM.avi
13.Sins.2014.1080p.BluRay.DTS.x264-HDS.mkv
A.Million.Ways.to.Die.in.the.West.2014.1080p.WEB-DL.x264.AC3-EVO.mkv
Ant-Man.2015.1080p.BluRay.x264-SPARKS.mkv";

string pattern = @"

^(?<Name>.+?) # Movie Name up to year and resolution
(?!\.[12]\d\d\d\.\d{,3}[ip]\.) # Year and resolution foward negative look ahead as an a pattern anchor
\. # Non captured due to only explicitly capturing.
(?<Year>\d\d\d\d) # Capture Year, etc...
\.
(?<Resolution>[^.]+)
\.
(?<Format>[^.]+)

";


Regex.Matches(data, pattern, RegexOptions.ExplicitCapture // Only what we ask for (?<> ), ignore non captures
| RegexOptions.Multiline // ^ makes each line a separate one.
| RegexOptions.IgnorePatternWhitespace) // Allows us to comment pattern only.
.OfType<Match>(
.Select (mt => new
{
Movie = Regex.Replace(mt.Groups["Name"].Value, @"\.", " "),
Year = mt.Groups["Year"].Value,
Resolution = mt.Groups["Resolution"].Value,
Format = mt.Groups["Format"].Value,
});

结果

enter image description here

关于下载电影的 C# 模式匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34336618/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com