如何在运行时划分字符串? [英] How can I delimitate a string at runtime?
问题描述
我希望编写一个实用程序,使用正则表达式一次批量重命名一堆文件。我将一次重命名的文件遵循某个命名约定,我想使用已经在文件名中的数据将它们更改为新的命名约定;但并非我的所有文件目前都遵循相同的约定。
所以我希望能够编写一个通用程序,让我在运行时输入到文本框中的模式文件名,以及我想从文件名中提取的用于重命名的标记。
例如 - 假设我有一个这样命名的文件:
[Coalgirls] _Suite_Precure_02_(1280x720_Blu-Ray_FLAC)_ [33D74D55] .mkv
我希望能够将此文件重命名为:
Suite Precure - Ep 02 [Coalgirls] [33D74D55] .mkv
这意味着我最好能够在重命名类似
[%group%] _ Suite_Precure_%ep%_(...)_ [%crc%]。mkv 并填充局部变量group,ep和crc以用于批量重命名。
我怎么可能接近这个?解决方案
使用常规ex压力组:
\ [(?< publisher>。*?)\] _Suite_Precure _(?< ep> \d +)_ \( (小于?组>?*)\)_\ [(小于?CRC>?*)\] .MKV< / CRC>< /组>< / EP>< /出版者> ;
这产生了4组,所以你的例子变成:
发布者CoalGirls
Ep 02
Group 12080x720_Blu-Ray_FLAC
CRC 33D74D55
然后您可以在Regex.Replace中使用这些来生成新名称:
{集团} _Suite_Precure _
{EP} _(
I am looking to write a utility to batch rename a bunch of files at once using a regular expression. The files that I will be renaming all at once follow a certain naming convention, and I want to alter them to a new naming convention using data that''s already in the filenames; but not all my files follow the same convention currently.
So I want to be able to write a general use program that lets me input into a textbox during runtime the pattern of the filename, and what tokens I want to extract from the filename to use for renaming.
For example - Assume I have one file named as such:
[Coalgirls]_Suite_Precure_02_(1280x720_Blu-Ray_FLAC)_[33D74D55].mkv
I want to be able to rename this file to:
Suite Precure - Ep 02 [Coalgirls][33D74D55].mkv
This means I would preferably be able to enter into my program before renaming something akin to
[%group%]_Suite_Precure_%ep%_(...)_[%crc%].mkv
and it would populate the local variables "group", "ep", and "crc" to use in the batch rename.
How might I approach this?
解决方案 Use the Regular expression groups:
\[(?<publisher>.*?)\]_Suite_Precure_(?<ep>\d+)_\((?<group>.*?)\)_\[(?<crc>.*?)\].mkv</crc></group></ep></publisher>
This produces 4 groups, so your example becomes:
Publisher CoalGirls
Ep 02
Group 12080x720_Blu-Ray_FLAC
CRC 33D74D55
You can then use these in the Regex.Replace to generate the new name:
{Group}_Suite_Precure_
{Ep}_(
这篇关于如何在运行时划分字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!