My experiance with regular expressions is confined to PHP where I only ever do a 'match', and not 'replace'. I'm far from being an expert.
In DOpus I want to create a 'Rename' preset to automatically rename files so they are 'web safe'. So basically, any special characters are stripped and spaces are replaced with hyphens.
But I'm not sure this is possible because every file will need to be broken down into an unknown number of parameters.
Is it possible to create a single rule that can rename any filename to be web safe?
[quote]My experiance with regular expressions is confined to PHP where I only ever do a 'match', and not 'replace'. I'm far from being an expert.
[/quote]
I think you are referring to the PHP preg_match() function.
The corresponding 'replace' function is ereg_replace().
I also am far from being an expert.
Regular Expressions in Directory Opus aren't really much different.
The objective of the task is of course a bit different.
You are asking to replace spaces with hyphens.
You also ask to strip special characters.
Do you mean replace Ä with A ?
I don't think this can be easily be done.
It is more difficult, but perhaps possible.
However, it is easy to replace Ä with a hyphen .
Here is a method I use.
Old Name:
(.*)(Ä|ö|ü| )(.*)(\.)(.*)#
New Name:
\1_\3\4\5
This can be simplified.
I simply modified code from a button I use.
Zippo, you're example highlights the issue I was trying to describe. You have a pattern that assumes a specific pattern of chunks of text. I'm looking for a regular expression that will work for a filename such as "hello world.txt" and "the quick brown fox jumps over the lazy dog.txt".
It doesn't need to 'convert' special characters. It should just remove them. So "an example% file$.txt" would become "an-example-file.txt".
[quote]I'm looking for a regular expression that will work for a filename such as "hello world.txt" and "the quick brown fox jumps over the lazy dog.txt".
[/quote]
You mean in one regular expression ?
I have something I think .
I only need to combine another result I have for something else.
I'll give it a try.
[quote]If you want to remove bad characters, except spaces, and turn spaces into - then I think you have to do it in two steps, from
(.)([^-0-9a-zA-Z._ ]+)(.)#
to
\1\3
[/quote]
Very interesting,
And my suggestion to accomplish this was from
(.)($|%)(.)(.)(.*)#
to
\1\3\4\5
which is essentially Logically Not what Nudel did.
They compare a bit better if I rewrite mine as
(.)([$%]+)(.)#
to
\1\3\
I worked a short while last night attempting to write the complete solution as one RegExp.
I have limited success, and an idea of how to proceed, but nothing worth posting yet.
So in the event that I fail to accomplish this, we're at an end here.
To quote Nudel:
Are my installs of DO messed up? When I open the 'rename' dialog and select 'Regular Expression' from the 'type' drop-down list, the two fields above still say "Old name" and "New name". I didn't think anything of it until I copied and pasted nudel's regular expression in to "Old name" and DO completely locked up, both on my home PC and work PC. Try it.
If I hand type the regex it copes ok, but when I put "\1\3" into the "New name" field it literelly changes the name to "\1\3". Am I missing something?
[quote="Zippo"]
(.)([$%]+)(.)#
to
\1\3[/quote]
Thanks for your efforts Zippo. But this expression would only match $ and %. I needed it to match all 'special characters', or any non-alphnumeric character.
If I can get around the DO bug I descibed above I think nudel's solution will work well.
[quote]I needed it to match all 'special characters', or any non-alphnumeric character.
[/quote]
Yes, for your application, Nudel's solution is the correct one to use.
[quote]When I open the 'rename' dialog and select 'Regular Expression' from the 'type' drop-down list, the two fields above still say "Old name" and "New name". I didn't think anything of it until I copied and pasted nudel's regular expression in to "Old name" and DO completely locked up, both on my home PC and work PC.
[/quote]
Somehow one of your selected filenames is not converging.
You're caught in an infinite loop.
My guess is that the culprit is New Name.
Try:
a)Enter the New Name field first.
If that doesn't work, try b).
b)Add an integer after the '#' character in the RegExp.
This will limit the number of iterations and prevent the 'lock up'.
Then, try to identify what filename is causing this.
Yeah, it's a bug in the current Opus. It was noticed it a little while back but I guess the fix hasn't made it into a release version yet (assuming you're using the latest version).
Zippo's suggestion will work around the problem. (Paste the New Name first, then the old name.)
The problem is due to the way the recursive regexp rename, triggered by the # character, decides when to stop recursing. It should only be the rename dialog which locks up -- the rest of Opus should continue to work -- but it'll probably slow your PC down until you kill the process because it'll be in a loop using up all of (one) CPU. Shouldn't cause loss of data but can be annoying.