Rename Files Mystery

You don't escape things within other things that are expanded. Just like a shell variable that contains quotes does not mean that the contents of that shell variable are somehow quoted. You only get that kind of behaviour when there are multiple layers of processing, which is not applicable in this case, unless you want to create the XML, which would be a possibility.

Standard regex what? Regex says nothing about replacement strings and there is precious little standard about replacement strings. Different apps don't even agree on how bracketed groups are referenced, how escaping (if any) works, what character replacements are supported.

This is comparing Apples to Oranges. Keyboard Maestro’s replacement string is a token text string which you can include tokens, backslashed characters and group identifiers in. Replaced tokens that include backslashed characters don't expand them, replaced tokens that include group identifiers don't replace them, replaced groups that contain tokens or other group identifiers don't replace them.

If you use another language, with some other regex, then it will have its own definition of a replacement string, and will vary from language to language. And if you pass it a string, it wont expand things within that string to other things that expand as well either. If the first group matches “$2" and you include $1 in the replace string, the result will be “$2”, not the match of the second group.

In the same way, in Keyboard Maestro, if the first group matches “%LongDate%” you replace with “$1” you’ll get “%LongDate%” not the result of that token. And if your replace string contains “%LongDate%” and the LongDate token happened to include the “$1” would would just get that date from the token, it would not include the contents of the first match.

The replacement string is a token string. It includes tokens, which may include variable - they are expanded and the results used verbatim. It includes $1, $2 etc, and they are expanded to the group they match, and those matches are used verbatim. Neither one is processed further to include other things that might need processing.

1 Like