c# - 用于匹配季节和剧集的正则表达式

标签 c# .net regex

我正在为自己制作一个小应用程序,我想找到与某个模式匹配的字符串,但找不到正确的正则表达式。

Stargate.SG-1.S01E08.iNT.DVDRip.XviD-LOCK.avi

这是我拥有的字符串的示例,我只想知道它是否包含 S[NUMBER]E[NUMBER] 的子字符串,每个数字最多 2 位。

你能给我一个线索吗?

最佳答案

正则表达式

Here是使用命名组的正则表达式:

S(?<season>\d{1,2})E(?<episode>\d{1,2})

用法

然后,您可以获得像这样的命名组(季和剧集):

string sample = "Stargate.SG-1.S01E08.iNT.DVDRip.XviD-LOCK.avi";
Regex  regex  = new Regex(@"S(?<season>\d{1,2})E(?<episode>\d{1,2})");

Match match = regex.Match(sample);
if (match.Success)
{
    string season  = match.Groups["season"].Value;
    string episode = match.Groups["episode"].Value;
    Console.WriteLine("Season: " + season + ", Episode: " + episode);
}
else
{
    Console.WriteLine("No match!");
}

正则表达式的解释

S                // match 'S'
(                // start of a capture group
    ?<season>    // name of the capture group: season
    \d{1,2}      // match 1 to 2 digits
)                // end of the capture group
E                // match 'E'
(                // start of a capture group
    ?<episode>   // name of the capture group: episode
    \d{1,2}      // match 1 to 2 digits
)                // end of the capture group

关于c# - 用于匹配季节和剧集的正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12082536/

相关文章:

c# - 如何从 .NET DataGridView 控件单元格值写入文本文件?

.net - MessageInspector消息: "This message cannot support the operation because it has been copied."

c# - 检查 WinForm 应用程序中输入的文本

c# - WPF DataGrid EditItem 异常

regex - 从动态输入中提取字符串,其中部分字符串可能不存在

c# - c# 中的正则表达式驼峰式

c# - 等待来自 Language-ext 的 OptionAsync<T>

c# - 如何访问 App.config 中的其他选项?

c# - 如何减少线程无限循环期间的处理器使用率?

javascript - 使用 javascript 或正则表达式从值中分割数字和字符串