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