您可以构造一个正则表达式字符串来解析季节和剧集编号,如下所示:
$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
}
}
}
输出:
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
正则表达式详细信息:
(?: # 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)
)
)
我已经更改了您的一些示例,以便更清楚地显示正确找到的数字