构建图像序列文件名正则表达式:捕获前缀、帧号及扩展名
Fixing Your Image Sequence Filename Regex
Hey there! Let's get that regex working exactly how you need it. You're already halfway there with your existing pattern—we just need to add the prefix capture group and tweak things to cover all your examples.
The Final Regex
^(.*)(\d+)\.([a-zA-Z0-9]{3,4})$
Breakdown of Each Capture Group
Let's walk through what each part does, so you understand how it fits your requirements:
- Group 1:
(.*)
This greedily captures everything from the start of the filename up to the last sequence of digits (your frame number). It works even if your prefix has other numbers, dots, underscores, or spaces—like inimg01_123.pngorimg 001.12.png—because the regex engine will automatically backtrack to leave the final digit sequence for the next group. - Group 2:
(\d+)
Captures one or more consecutive digits (your frame number), which sits right before the extension's dot—exactly what you need since the frame number is always adjacent to the extension. - Group 3:
([a-zA-Z0-9]{3,4})
Captures the 3-4 character extension (letters or numbers, covering common types likepng,jpg,jpeg). The\.before it ensures we only match the dot immediately preceding the extension.
Testing Against Your Examples
Let's verify it works with all your sample filenames:
img123.png→ Group 1:img, Group 2:123, Group 3:pngimg.123.png→ Group 1:img., Group 2:123, Group 3:pngimg_v01_1234.png→ Group 1:img_v01_, Group 2:1234, Group 3:pngimg01_123.png→ Group 1:img01_, Group 2:123, Group 3:pngimg.001.123.png→ Group 1:img.001., Group 2:123, Group 3:pngimg 001.12.png→ Group 1:img 001., Group 2:12, Group 3:png
All cases check out! If you ever need to adjust for edge cases (like extensions with special characters), you can modify the third group's character set, but this should cover your stated requirements perfectly.
内容的提问来源于stack exchange,提问作者Spencer
相关产品推荐
相关产品推荐

