检查苹果脚本中是否包含四位数字年份
Check if contains four digits year number in apple script
现在我正在研究 file-rename-applescript-project
。这是一个例子:The.Fantasy.1997.DVDRip.XviD-ETRG.avi
.
现在我想检查文件名是否包含四位数字年份。在本例中,它是 1997 年。年份数字必须以 19 或 20 开头,并且必须包含四位数字。
如果结果为真我会做一些事情,如果结果为假我会做其他事情。
我尝试使用正则表达式但找不到解决方案。这超出了我的范围。现在我在这里寻求帮助,谢谢一百万。
正则表达式 .+\.(?:19:20)\d{2}\..+
应该做到
细分:
.+
1个或多个任意字符
\.
一个实际的点
(?:19|20)
字符串“19”或“20”(非捕获组)
\d{2}
正好两位数
\.
一个实际的点
.+
1 个或多个任意字符
如果您想完全避免使用正则表达式,请执行如下操作,使用文本项分隔符:
(*
This first bit breaks the string up into a list of words by cutting the string
at the period delimiter.
*)
set tid to my text item delimiters
set my text item delimiters to "."
set bits_list to text items of file_name_string
set my text item delimiters to tid
(*
This repeat loop goes though the list of words and tests them (first) to see
if it can be converted to an integer, and (second) whether the number is between
1900 and 2100. If so, it chooses it as the year.
*)
repeat with this_item in bits_list
try
set possibleYear to this_item as integer
if possibleYear ≥ 1900 and possibleYear < 2100 then
-- do what you want with the year value here
exit repeat
end if
end try
end repeat
当然,如果名称中有数字(例如“2001.A.Space.Odyssey.1968.avi”)或者文件名有不同的分隔符(例如 space 或破折号)。但是您也会 运行 使用正则表达式解决这些问题,所以...
因为您只想检查文件名是否包含 1900-2099 范围内的 four-digit 年份,您可以通过定义一个处理程序来非常简单地做到这一点:
on hasYearInTitle(filmTitle as text)
repeat with yyyy from 1900 to 2099
if yyyy is in the filmTitle then return true
end repeat
return false
end hasYearInTitle
然后您可以调用此处理程序并将电影标题传递给它,如下所示:
hasYearInTitle("The.Fantasy.1997.DVDRip.XviD-ETRG.avi") --> true
hasYearInTitle("The.Fantasy.197.DVDRip.XviD-ETRG.avi") --> false
hasYearInTitle("2001.A.Space.Odyssey.1968.avi") --> true
hasYearInTitle("2001.A.Space.Odyssey.avi") --> true (hm...)
作为 side-note,由 newznab 服务器索引的电影遵循严格的 file-naming 协议,允许媒体服务器(在您的机)轻松解析并快速提取信息,涉及(如您的示例文件名中所示):电影的标题、电影的发行日期、来源 material、编码质量、编码格式(编解码器) 、发布组和包含的文件格式。
虽然有些文件名包含更多信息,但有些文件名应该始终按固定顺序出现。这使得它们在您需要时非常容易自己解析,但如果您正在寻找创建一个有组织的媒体库,您最好考虑使用媒体服务器,其中有优秀的免费软件 long-standing 软件适用于 macOS 和几乎所有其他操作系统的选项。
现在我正在研究 file-rename-applescript-project
。这是一个例子:The.Fantasy.1997.DVDRip.XviD-ETRG.avi
.
现在我想检查文件名是否包含四位数字年份。在本例中,它是 1997 年。年份数字必须以 19 或 20 开头,并且必须包含四位数字。
如果结果为真我会做一些事情,如果结果为假我会做其他事情。 我尝试使用正则表达式但找不到解决方案。这超出了我的范围。现在我在这里寻求帮助,谢谢一百万。
正则表达式 .+\.(?:19:20)\d{2}\..+
应该做到
细分:
.+
1个或多个任意字符
\.
一个实际的点
(?:19|20)
字符串“19”或“20”(非捕获组)
\d{2}
正好两位数
\.
一个实际的点
.+
1 个或多个任意字符
如果您想完全避免使用正则表达式,请执行如下操作,使用文本项分隔符:
(*
This first bit breaks the string up into a list of words by cutting the string
at the period delimiter.
*)
set tid to my text item delimiters
set my text item delimiters to "."
set bits_list to text items of file_name_string
set my text item delimiters to tid
(*
This repeat loop goes though the list of words and tests them (first) to see
if it can be converted to an integer, and (second) whether the number is between
1900 and 2100. If so, it chooses it as the year.
*)
repeat with this_item in bits_list
try
set possibleYear to this_item as integer
if possibleYear ≥ 1900 and possibleYear < 2100 then
-- do what you want with the year value here
exit repeat
end if
end try
end repeat
当然,如果名称中有数字(例如“2001.A.Space.Odyssey.1968.avi”)或者文件名有不同的分隔符(例如 space 或破折号)。但是您也会 运行 使用正则表达式解决这些问题,所以...
因为您只想检查文件名是否包含 1900-2099 范围内的 four-digit 年份,您可以通过定义一个处理程序来非常简单地做到这一点:
on hasYearInTitle(filmTitle as text)
repeat with yyyy from 1900 to 2099
if yyyy is in the filmTitle then return true
end repeat
return false
end hasYearInTitle
然后您可以调用此处理程序并将电影标题传递给它,如下所示:
hasYearInTitle("The.Fantasy.1997.DVDRip.XviD-ETRG.avi") --> true
hasYearInTitle("The.Fantasy.197.DVDRip.XviD-ETRG.avi") --> false
hasYearInTitle("2001.A.Space.Odyssey.1968.avi") --> true
hasYearInTitle("2001.A.Space.Odyssey.avi") --> true (hm...)
作为 side-note,由 newznab 服务器索引的电影遵循严格的 file-naming 协议,允许媒体服务器(在您的机)轻松解析并快速提取信息,涉及(如您的示例文件名中所示):电影的标题、电影的发行日期、来源 material、编码质量、编码格式(编解码器) 、发布组和包含的文件格式。
虽然有些文件名包含更多信息,但有些文件名应该始终按固定顺序出现。这使得它们在您需要时非常容易自己解析,但如果您正在寻找创建一个有组织的媒体库,您最好考虑使用媒体服务器,其中有优秀的免费软件 long-standing 软件适用于 macOS 和几乎所有其他操作系统的选项。