我正在为自己制作小应用程序,我想找到与模式匹配的字符串,但我找不到合适的正则表达式.
Stargate.SG-1.S01E08.iNT.DVDRip.XviD-LOCK.avi
Run Code Online (Sandbox Code Playgroud)
这是我所拥有的字符串的expamle,我只想知道它是否包含S [NUMBER] E [NUMBER]的子字符串,每个数字最多2位数.
你能给我一个线索吗?
这是使用命名组的正则表达式:
S(?<season>\d{1,2})E(?<episode>\d{1,2})
Run Code Online (Sandbox Code Playgroud)
然后,您可以像这样获得命名组(赛季和剧集):
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!");
}
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)