Currently I need to do some bulk transformation of some file names.
I have, say, 3 file names: 2.640-849.0.jpg, g2650ohr.jpg, and k2.26mr.jpg (so these are treated as three separate strings)
I want a (hopefully) single regex to substitute all of the . (periods) with - (dashes) except for the .jpg extension.
A first draft would be
but that gives you 2-640-849-0-jpg. Note the extension is transformed as well.
I could then do a second regex like s/-jpg/.jpg/ but that feels less than optimal.
The solution is a zero-width negative look-ahead assertion
man perlre for more information on this, but the basic idea is that
you match occurances of the preceeding expression that ate not followed by the zwnla.
For example /foo(?!bar)/ matches any occurrence of foo that isn’t followed by bar.
Combine this with the common perl utility rename and we get the following:
rename -n 's/\.(?!jpg)/-/g' *
Note that Matz has decided not to include the zero-width negative look-ahead assertion to Ruby.
Thanks goes out to Matt Pulver for submitting these improvements to the above regex:
This will handles cases like test.jpg.jpg
If you want to substitute all non-alpha-numeric characters and support other file extensions try: