Regular Expressions + Find & Replace -- Boolean Error

Using the latest Directory Opus 12.18 (x64)

I spent a lot of time banging my head against the wall to determine this was a Directory Opus error (when I had assumed it was my mistake)...

When using the "Rename > Regular Expressions + Find & Replace" feature:

If you add too many grouped criteria with booleans, Directory Opus will replace the wrong section of the filename -- or -- add a number into the filename, rather than using only the text supplied as the intended replacement name.

To simplify the process of reproducing this, I will simplify the regex and provide you the threshold where it breaks. The regex string I'm pasting would not be practical normally; however, in this case it's merely a way to provide you an easier-to-read option.

Create a sample file: "Keep-ThisReplace-This.txt"

Take a regex expression like this:

(Replace|zzz)-(This)
Leave the text-replcement field blank (do the same for all subsequent tests, below)

The above works fine. Now add a bit more:

(12|34)(aaa|bbb)|(56|78)(ccc|ddd)|(Replace|zzz)-(This)

This will replace the entire filename in many cases, rather than only the "Replace-This" component. You can test any regex tool to confirm that this should not be the case.

Now add one more group:

(new|group)(added|here)|(12|34)(aaa|bbb)|(56|78)(ccc|ddd)|(Replace|zzz)-(This)

Rather than replacing the text, it will be replaced with a number which appears to be some sort of index field. In this case it would replace the text "Replace-This" with a "0" (zero).

...I've done my best to simplify this, let me know if it doesn't make sens and I'll attempt to explain in more detail.

You have some | outside of brackets in the non-working regex. Haven't had a chance to check but those may be the cause of the problem.

According to Regexper probably Ok Regexper, maybe a wrong RegEx dialect used? Which one does DO use, did not look into the docu...

Thank you for the reply. Just to clarify, I am a developer and take care with my examples.

To make the error easier to illustrate, I've setup extremely basic/simple regex syntax, which is supported by any/all regular expression variants (pipes and parenthesis being the only symbols used here).

It's also correct, there are no extra pipe's. It might be easier to review the syntax if you paste into a tool like https://regexr.com/ (also suggested by Micky).

I spent a couple hours proofing these issues exist only in Directory Opus and have tested dozens of variations of this expression in other tools, as well as Directory Opus, and other syntax checkers. This is absolutely a Directory Opus specific issue, from all the time I have invested testing.

Thank you.

1 Like

It works fine if you add the missing outer brackets:

(Although I think this is still an ambiguous regex. I'd add even more brackets than this. e.g. I don't know if
(12|32)(aaa|bbb)|(56|78)(ccc|ddd) means
(12|32)((aaa|bbb)|(56|78))(ccc|ddd) or
((12|32)(aaa|bbb))|((56|78)(ccc|ddd)) or something else... But maybe if you know the regex rules really well you don't need them, I don't know.)

1 Like

Although I think this is still an ambiguous regex.

I might not have done a good job explaining the first time around, or you may have overlooked it -- however, I explained that to simplify the process and make it easier to see the problem, I simplified the regular expression in hopes of making it easier/quicker for you.

This was meant to be of help -- and as I explained, it might make less sense as a result -- but it's a heck of a lot easier than quoting the practical example that's 4x as long.

you don't need them

Correct, you wouldn't normally need them. However, if these are the rules of Directory Opus, that's no problem. I'll start wrapping any boolean expression (eg - any expression with pipes) with a final set of parenthesis.

Because this is not generally an issue elsewhere, it may be worth adding that to the help file for others "When using boolean logic, for example a pipe character between two alternative expressions (abc|123), the login must exist within parenthesis."

Obviously that doesn't help me now that I've reached this point; I'm merely offering it to help others.

In the Regex + Find & Replace mode there is some transformation of the regex (and replacement string) that goes on behind the scenes, which complicates things a bit.

You could use a rename script instead if you want different rules. (Those can add input fields of their own, which could take a regex and apply it in different ways, if you wanted, without having to edit the script each time the regex changed.)

First, sorry, type-o in my help-text "the *logic must be placed between parenthesis."

Thank you for the suggestion. To be sure I'm not misinterpreting your suggestion -- you are referring to using the VB script option and applying the regex within the VB script?

I've made a change for the next version so that the extra brackets are no longer required:

(It adds the brackets for you.)

The issue when adding more groups is due to having more than 7 capture groups (9 in total; we add two groups of our own to capture the start and end of the string).

For now, that can be avoided by using a non-capturing group for some/all of the groups:

But in the next version, that will be handled correctly without the need for doing that, and your original example will work as-is:

That sounds amazing! Thank you for the hard work and quick follow-up!

1 Like