I have a group of files with names of varying length in the format: aaaa-1234-filename.zip, aaaa-12345-filename-still-filename.zip, aaaa-123456-filename.zip, aaaa-1234567-filename-still-filename.zip.
I would like to strip off the first part of these filenames and remove everything up to and including the second -. I have attempted to go through PowerShell, but the Rename-Item -NewName only seems to be useful for removing the standardized portion of the name (e.g., aaaa-) and not the variable-length parts of the names.
Am I missing something in PowerShell or is there a better, easier way to perform this task?
There are a couple of ways to do this:
'\d+'
: matches all numbers,'(?<=-).*?(?=-)'
: splits using -
delimiter,str.split('-')
: splits using -
delimiter.Examples:
(1)
import re
s = 'aaaa-12345-filename-still-filename.zip'
nums = re.findall(r'\d+', s)[0]
# nums = '12345'
(2)
import re
s = 'aaaa-12345-filename-still-filename.zip'
nums = re.findall('(?<=-).*?(?=-)', s)[0]
# nums = '12345'
(3)
import re
s = 'aaaa-12345-filename-still-filename.zip'
nums = s.split('-')[1]
# nums = '12345'