【问题标题】:Delphi - how to check if a string contains a set format of charactersDelphi - 如何检查字符串是否包含一组字符格式
【发布时间】:2015-12-21 22:43:26
【问题描述】:

我试图从我的电影中分类我的电视节目,我认为最好的方法是通过电视节目的季节和剧集标记来识别它们。

我所有的电视节目都有以下格式 S00E00。

尝试了以下

if (Pos(IntToStr(I), SR.Name) > 0) and (Pos('S', SR.Name) > 0) then
result := true;

但这不起作用,因为如果一部电影的标题包含“s”和任何数字,它会复制它。

需要类似的东西 如果字符串 pos "字母 s 后跟整数 , 整数 , 字母 e , 整数 , 整数" 那么结果 := true

那是不管剧集是 S01E03 还是 S09E12 仍然会复制。

.......................

2015 年 12 月 22 日编辑

.......................

谢谢 Remy 没想到会这么容易。

这里只是为了澄清的过程。

procedure TForm7.TvShowCopy (Format: string);// Format would be .mp4 or whatever you're using but since the procedure searches for a name matching "S00E00" format I suppose you wouldn't need the format and could just use *.*. 

begin

  aTvshowFiles := TDirectory.GetFiles(STvshows,format,// aTVshowfiles  is a TStringDynArray , STvshows is the Source directory for the TV shows.
                   TSearchOption.soAllDirectories,
                   function(const Path: string; const SR: TSearchRec):Boolean
                   begin
                     result:= TRegEx.IsMatch(SR.Name, 'S[0-9][0-9]E[0-9][0-9]') 
                   end);
                     CopyFilesToPath(aTvshowFiles, DTvshows);
end;

经过测试,似乎可以正常工作。

【问题讨论】:

  • 这听起来像是正则表达式的工作。
  • 什么Delphi版本?数据存储在哪里(内存、数据库或其他地方)?您需要添加更多详细信息。请edit您的问题这样做,而不是通过 cmets 添加它们。
  • @KenWhite: SR.Name 暗示TSearchRec 可能正在被使用,这意味着正在使用文件名枚举。
  • @Remy:不必推断。如果发布者尝试匹配文件名,则与尝试匹配数据库中的数据非常不同。如果对海报进行编辑以明确提出的问题,那么对于未来的读者来说,这个问题将会得到改善。如果问题是关于匹配文件名,那么它不应该询问检查 string;它应该询问是否检查 filename.

标签: delphi


【解决方案1】:

正如 Ron 在 cmets 中提到的,您可以使用 regular expression

uses
  ..., RegularExpressions;

function ContainsSeasonAndEpisode(const Input: String): Boolean;
begin
  Result := TRegEx.IsMatch(Input, 'S[0-9][0-9]E[0-9][0-9]'); // or: 'S\d\dE\d\d'
end;

...

Result := ContainsSeasonAndEpisode(SR.Name);

正如Ken在cmets中提到的,你也可以使用MatchesMask(),比如如果你正在测试一个文件名:

uses
  ..., Masks;

function ContainsSeasonAndEpisode(const Input: String): Boolean;
begin
  Result := MatchesMask(Input, '*S[0-9][0-9]E[0-9][0-9]*');
end;

...

Result := ContainsSeasonAndEpisode(SR.Name);

或者,您可以简单地手动比较输入字符串,例如:

uses
  ..., StrUtils;

function ContainsSeasonAndEpisode(const Input: String): Boolean;
begin
  Result := false;
  Idx := Pos('S', Input);
  while Idx <> 0 do
  begin
    if (Idx+5) > Length(Input) then
      Exit;

    if (Input[Idx+1] >= '0') and (Input[Idx+1] <= '9') and
       (Input[Idx+2] >= '0') and (Input[Idx+2] <= '9') and
       (Input[Idx+3] = 'E') and
       (Input[Idx+4] >= '0') and (Input[Idx+4] <= '9') and
       (Input[Idx+5] >= '0') and (Input[Idx+5] <= '9') then
    begin
      Result := true;
      Exit;
    end;

    Idx := PosEx('S', Input, Idx+1);
  end;
end;

...

Result := ContainsSeasonAndEpisode(SR.Name);

【讨论】:

  • 根据信息是搜索以匹配特定模式中的文件名,MatchesMask 也可能是替代方案 - 它支持伪正则表达式类型模式匹配。
猜你喜欢
  • 2017-01-18
  • 2014-09-17
  • 1970-01-01
  • 2017-09-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-14
相关资源
最近更新 更多