Skip to content

Commit 39547e5

Browse files
committed
Add support to automatically match characters that are not only numbers.
1 parent 8e36abc commit 39547e5

1 file changed

Lines changed: 77 additions & 16 deletions

File tree

SubtitleRenamer/Action.cs

Lines changed: 77 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -158,12 +158,15 @@ private void _MatchEpisode()
158158

159159
SetCurtStatus("匹配集数中...");
160160

161-
int beginPos;
162-
int subBeginPos;
161+
int beginPos, subBeginPos;
162+
string endStr, subEndStr;
163163
try
164164
{
165-
beginPos = GetEpisodePosByTwoStr(VideoFileList[0].Name, VideoFileList[1].Name); // 视频文件名集数开始位置
166-
subBeginPos = GetEpisodePosByTwoStr(SubtitleFileList[0].Name, SubtitleFileList[1].Name); // 字幕文件名集数开始位置
165+
beginPos = GetEpisodePosByList(VideoFileList); // 视频文件名集数开始位置
166+
subBeginPos = GetEpisodePosByList(SubtitleFileList); // 字幕文件名集数开始位置
167+
168+
endStr = GetEndStrByList(VideoFileList, beginPos);
169+
subEndStr = GetEndStrByList(SubtitleFileList, subBeginPos);
167170
}
168171
catch
169172
{
@@ -174,11 +177,11 @@ private void _MatchEpisode()
174177
// 集数字典更新
175178
VideoEpisDict.Clear();
176179
foreach (var file in VideoFileList)
177-
VideoEpisDict[file.Name] = GetEpisByFileName(file.Name, beginPos: beginPos);
180+
VideoEpisDict[file.Name] = GetEpisByFileName(file.Name, beginPos: beginPos, endStr: endStr);
178181

179182
SubtitleEpisDict.Clear();
180183
foreach (var file in SubtitleFileList)
181-
SubtitleEpisDict[file.Name] = GetEpisByFileName(file.Name, beginPos: subBeginPos);
184+
SubtitleEpisDict[file.Name] = GetEpisByFileName(file.Name, beginPos: subBeginPos, endStr: subEndStr);
182185

183186
// 生成改名字典
184187
SubtitleRenameDict.Clear();
@@ -202,6 +205,34 @@ private void _MatchEpisode()
202205
RefreshFileListBox();
203206
}
204207

208+
// 遍历所有 list 中的项目,尝试得到集数开始位置
209+
private int GetEpisodePosByList(List<FileInfo> list)
210+
{
211+
int aIndex = 0;
212+
int bIndex = 1;
213+
int beginPos = -1;
214+
215+
while (true)
216+
{
217+
try
218+
{
219+
int result = GetEpisodePosByTwoStr(list[aIndex].Name, list[bIndex].Name);
220+
beginPos = result;
221+
break;
222+
}
223+
catch
224+
{
225+
aIndex++;
226+
bIndex++;
227+
if (aIndex >= list.Count || bIndex >= list.Count) break;
228+
}
229+
}
230+
231+
if (beginPos == -1) throw new Exception("beginPos == -1");
232+
233+
return beginPos;
234+
}
235+
205236
// 通过比对两个文件名中 数字 不同的部分来得到 集数 的位置
206237
private int GetEpisodePosByTwoStr(string strA, string strB)
207238
{
@@ -221,23 +252,53 @@ private int GetEpisodePosByTwoStr(string strA, string strB)
221252
}
222253
}
223254

224-
if (beginPos == -1)
225-
{
226-
throw new Exception("beginPos == -1");
227-
}
255+
if (beginPos == -1) throw new Exception("beginPos == -1");
228256

229257
return beginPos;
230258
}
231259

232-
/// 获取集数
233-
private string GetEpisByFileName(string fileName, int beginPos)
260+
// 获取集数
261+
private string GetEpisByFileName(string fileName, int beginPos, string endStr)
234262
{
235-
if (beginPos <= -1)
236-
return null;
237-
263+
if (beginPos <= -1) return null;
238264
var str = fileName.Substring(beginPos);
239265
var grp = Regex.Matches(str, @"(\d+)");
240-
return (grp.Count >= 1) ? grp[0].Value : null; // 为了获得完整的数字,无论多少位
266+
if (grp.Count <= 0 || grp[0].Index != 0)
267+
{
268+
var result = "";
269+
// 通过 endStr 获得集数
270+
for (int i = 0; i < str.Length; i++)
271+
{
272+
if (str[i].ToString() == endStr) break;
273+
result += str[i];
274+
}
275+
return result;
276+
277+
}
278+
return grp[0].Value; // 为了获得完整的数字,无论多少位
279+
}
280+
281+
// 获取终止字符
282+
private string GetEndStrByList(List<FileInfo> list, int beginPos)
283+
{
284+
string fileName = list.Where(o => Regex.IsMatch(o.Name.Substring(beginPos)[0].ToString(), @"^\d+$")).ToList()[0].Name; // 获取开始即是数字的文件名
285+
var grp = Regex.Matches(fileName, @"(\d+)");
286+
if (grp.Count <= 0) return null;
287+
Match firstNum = grp[0];
288+
int afterNumStrIndex = firstNum.Index + firstNum.Length; // 数字后面的第一个字符 index
289+
290+
// 不把特定字符(空格等)作为结束字符
291+
string strTmp = fileName.Substring(afterNumStrIndex);
292+
string result = null;
293+
for (int i = 0; i < strTmp.Length; i++)
294+
{
295+
if (strTmp[i].ToString() != " ")
296+
{
297+
result = strTmp[i].ToString();
298+
break;
299+
}
300+
}
301+
return result;
241302
}
242303

243304
protected void StartRename() => Task.Factory.StartNew(() => _StartRename());

0 commit comments

Comments
 (0)