简体   繁体   English

如何从标题中获取电视节目集和会话编号

[英]How to get tv show episode and session number from title

I'm try to get tvhsow season and episode number from the title我正在尝试从标题中获取 tvhsow 季节和剧集编号

I try following code which works but it also picking up title like xxxxe 3 as episode 3我尝试以下有效的代码,但它也将标题如 xxxxe 3 作为第 3 集

$episode = $title | Select-String -Pattern "E(\d+)", "E (\d+)", "Episode (\d+)" | % {$_.Matches.Groups[1].Value}
$season = $title | Select-String -Pattern "S(\d+)", "S (\d+)", "Season (\d+)" | % {$_.Matches.Groups[1].Value} 

How to i make sure that I can pick up the season number and episode from any of these formats.如何确保我可以从这些格式中的任何一种中获取季号和剧集。

  • xxx S01E01 xxx S01E01
  • xxxe 1 S01E01 xxxe 1 S01E01
  • xxx S01 E01 xxx S01 E01
  • xxx 01x01 xxx 01x01
  • xxx Season 01 Episode 01 xxx 季节 01 第 01 集

Assuming the following假设以下

  1. Seasons and episodes will always be 2 (or more) numbers季节和剧集将始终是 2 个(或更多)数字
  2. Seasons and episodes will always be at the end of the filename.季节和剧集将始终位于文件名的末尾。

I would recommend anchoring to the end of the name with the regex pattern.我建议使用正则表达式模式锚定到名称的末尾。 From there we account for 0 or more characters before a period (file extension), 1 literal period (for the extension), 0 or more characters between the period and the episode, and 0 or more characters between the season and the episode.从那里我们考虑了句点之前的 0 个或更多字符(文件扩展名)、1 个文字句点(用于扩展名)、句点和剧集之间的 0 个或更多字符以及季节和剧集之间的 0 个或更多字符。

$examples = @'
xxx S01E02.avi
xxxe 1 S02E03.mp3
xxx S03 E04.mov
xxx 04x05.png
xxx Season 05 Episode 06.wav
'@ -split [environment]::NewLine

$examples | ForEach-Object {
    if($_ -match '.+(\d{2,}).*(\d{2,}).*\..*$'){
        "Season: {0}  Episode: {1}" -f $matches.1,$matches.2
    }
}

This will output这将输出

Season: 01  Episode: 02
Season: 02  Episode: 03
Season: 03  Episode: 04
Season: 04  Episode: 05
Season: 05  Episode: 06

You didn't show how you populated $title, so it was assumed to just be a string.您没有显示如何填充 $title,因此假定它只是一个字符串。 However if you wanted to apply to file objects, you have a couple of options.但是,如果您想应用于文件对象,您有几个选择。

We can leave the regex pattern alone and use the Name property.我们可以单独使用正则表达式模式并使用 Name 属性。

$videolist = Get-Childitem -Path path\to\movies -Filter *.whatever

foreach($video in $videolist){
    if($video.Name -match '.+(\d{2,}).*(\d{2,}).*\..*$'){
        "Season: {0}  Episode: {1}" -f $matches.1,$matches.2
    }
}

or或者

We can use the BaseName property and adjust the regex slightly.我们可以使用 BaseName 属性并稍微调整正则表达式。

$videolist = Get-Childitem -Path path\to\movies -Filter *.whatever

foreach($video in $videolist){
    if($video.BaseName -match '.+(\d{2,}).*(\d{2,}).*$'){
        "Season: {0}  Episode: {1}" -f $matches.1,$matches.2
    }
}

You could construct a regex string that parses out the season and episode numbers like this:您可以构建一个正则表达式字符串来解析这样的季节和剧集编号:

$examples = 'xxx S01E01','xxxe 1 S01E03','xxx S06 E01','xxx 01x01','xxx Season 01 Episode 02'

foreach ($title in $examples) {
    if ($title -match '(?:(?:S(?:eason)?)?\s*(\d+)[\sx]*)(?:(?:E(?:pisode)?)?\s*(\d+))') {
        $season  = [int]$matches[1]
        $episode = [int]$matches[2]

        # just to display the output:
        [PsCustomObject]@{
            Title   = $title
            Season  = $season
            Episode = $episode
        }
    }
}

Output:输出:

Title                    Season Episode
-----                    ------ -------
xxx S01E01                    1       1
xxxe 1 S01E03                 1       3
xxx S06 E01                   6       1
xxx 01x01                     1       1
xxx Season 01 Episode 02      1       2

Regex details:正则表达式详细信息:

(?:                # Match the regular expression below
   (?:             # Match the regular expression below
      S            # Match the character “S” literally
      (?:          # Match the regular expression below
         eason     # Match the characters “eason” literally
      )?           # Between zero and one times, as many times as possible, giving back as needed (greedy)
   )?              # Between zero and one times, as many times as possible, giving back as needed (greedy)
   \s              # Match a single character that is a “whitespace character” (spaces, tabs, line breaks, etc.)
      *            # Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
   (               # Match the regular expression below and capture its match into backreference number 1
      \d           # Match a single digit 0..9
         +         # Between one and unlimited times, as many times as possible, giving back as needed (greedy)
   )
   [\sx]           # Match a single character present in the list below
                   # A whitespace character (spaces, tabs, line breaks, etc.)
                   # The character “x”
      ?            # Between zero and one times, as many times as possible, giving back as needed (greedy)
)
(?:                # Match the regular expression below
   (?:             # Match the regular expression below
      E            # Match the character “E” literally
      (?:          # Match the regular expression below
         pisode    # Match the characters “pisode” literally
      )?           # Between zero and one times, as many times as possible, giving back as needed (greedy)
   )?              # Between zero and one times, as many times as possible, giving back as needed (greedy)
   \s              # Match a single character that is a “whitespace character” (spaces, tabs, line breaks, etc.)
      *            # Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
   (               # Match the regular expression below and capture its match into backreference number 2
      \d           # Match a single digit 0..9
         +         # Between one and unlimited times, as many times as possible, giving back as needed (greedy)
   )
)

I have changed some of your examples to make it clearer the numbers are correctly found我已经更改了您的一些示例,以便更清楚地找到正确的数字

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM