# Script to perform multiple Regular Expressions

**URL:** <https://resource.dopus.com/t/script-to-perform-multiple-regular-expressions/3384>\
**Category:** Rename Presets\
**Tags:** script, regex, vbscript, jscript\
**Created:** [April 29, 2007, 10:53pm UTC](https://resource.dopus.com/t/script-to-perform-multiple-regular-expressions/3384 "2007-04-29T22:53:24Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Leo](https://resource.dopus.com/user_avatar/resource.dopus.com/leo/32/69732_2.png) [@Leo](https://resource.dopus.com/u/Leo)\
**Post date:** [April 29, 2007, 10:53pm UTC](https://resource.dopus.com/t/script-to-perform-multiple-regular-expressions/3384/1 "2007-04-29T22:53:24Z")

</div>

Introduction

It's fairly common to want to apply several regular expressions to a filename, one after another.

While you can do this using several Rename commands in a row, that isn't ideal. Using multiple Rename commands would means you could not save the operation as a Rename Preset to use from the Rename Dialog. It is also slower as each would be renamed multiple times (once per Rename command), instead of calculating the final name of each file and renaming it once.

Instead of using multiple Rename commands, we use a rename script.

**Background reading**

- [Rename scripts in JScript, VBScript and PerlScript](https://resource.dopus.com/t/rename-scripts-in-jscript-vbscript-and-perlscript/5480), to learn more about rename scripts.
- [RegExp basics: Removing characters from start/end of names](https://resource.dopus.com/t/regexp-basics-removing-characters-from-start-end-of-names/5400), to learn the basics of regular expressions.
- Your favorite JScript or VBScript guide, to learn about regular expressions in your scripting language of choice. (JavaScript guides are much easier to find these days, and generally apply to JScript fine, although there are a few differences.)

**Example rename presets**

Attached are rename presets which contain the example scripts, which you can use as starting points. These are the basis for the rest of the post.

The versions of the script for Opus 12 & above are slightly better, but use features which will not work with earlier versions.

- Opus 12 & above, **JScript** : [Multi-RegExp (O12 JS).orp](https://resource.dopus.com/uploads/default/original/3X/0/1/011f0d88c020df5ece6ff30af41479eec8b6f30b.orp) (1.4 KB)
- Opus 12 & above, **VBScript** : [Multi-RegExp (O12 VBS).orp](https://resource.dopus.com/uploads/default/original/3X/8/7/8747320eac1a6d6f90c87b5e5401d035ff8b2bd0.orp) (1.7 KB)
- Opus 11 & earlier, **VBScript** : [Multi-RegExp.orp](https://resource.dopus.com/uploads/default/original/3X/8/1/813f7ce36bdf0f1053ac6c2750a9a08022946695.orp) (1.72 KB)
- See: [How to use Rename Presets from this forum](https://resource.dopus.com/t/how-to-use-rename-presets-from-this-forum/26726/3)

What the rename script does

Each preset has a script which does approximately the same thing, for different scripting languages or versions of Opus.

Each script, as it is, doesn't do anything very useful. The point is that it's a simple demonstration that you can adapt to do whatever you need.

Each script will do this:

- Convert the filename to lowercase. (Using the `LCase` in VBScript or `toLowerCase` in JScript.)
- Uppercase any occurrences of the word 'ms'. (Using the respective RegExp objects in each language.)
- Uppercase any occurrences of the word 'xp'. (Ditto.)
- Convert the word "windows" to "Windows". (Ditto. _Not done by the old Opus 11 version._)

The first two conversions are done case-sensitive, while the final one is case-insensitive (not that it really matters in the example, as the whole string is converted to lower-case first).

Opus 12 JScript:

 ![Opus12_JS](https://resource.dopus.com/uploads/default/original/3X/b/d/bddd08cf46e78cb22757fd2f129926806edd67b0.png)

Opus 12 VBScript:

 ![Opus12_VBS](https://resource.dopus.com/uploads/default/original/3X/5/e/5eb4831021f0311d21f117938bef03cbad48d8cc.png)

RegExp syntax in JScript and VBScript

The syntax of regular expressions differs slightly between Opus and JScript/VBScript. (Sadly, there is no one standard regexp syntax and you'll find differences between almost any pair of tools.) In particular, the replacement strings use `$1`, `$2`, etc. in both scripting languages, instead of the `\1`, `\2`, etc. used in Opus.

A simple example

[This thread](http://resource.dopus.com/viewtopic.php?t=12009) has a simple, real-world example of a similar multi-regex script being used to solve a slightly different problem.

A more complex example

Another thread, [Help needed, Spliting filenames into folders](http://resource.dopus.com/viewtopic.php?t=11498), uses a rename script which based on this one but with conditional branching to solve a more complex/esoteric problem.

Replacing multiple matches with a single expression:

Here is an aspect of regular expressions I had not noticed when first writing this example. Even in 'global' mode, a regexp will not repeatedly/recursively apply the regexp to replace multiple occurrences of something if the pattern matches the entire string.

For example, this only replaces one `%20` with a space:

```vbscript
re.Pattern = "(.*)%20(.*)"
str = re.Replace(str, "$1 $2")

```

![](https://resource.dopus.com/uploads/default/original/3X/a/8/a8da0750fb2b1f13277e6e70e7bd644f09c8d5e8.png)

To replace all `%20` with spaces, you have to use an expression with a smaller scope, like this:

```vbscript
re.Pattern = "%20"
str = re.Replace(str, " ")

```

![](https://resource.dopus.com/uploads/default/original/3X/7/7/77e5bbbeb205434e35b3b4c07f0fe617d791cf21.png)

(This is different to the special `#` syntax you can add to the end of a regular expression in the Opus rename dialog. The `#` at the end is an Opus-specific addition to the standard regexp syntax, and will make Opus apply the expression over and over until either the string stops changing or it starts generating a loop of results which have already been seen.)

---

<div class="post-metadata">

**Author:** ![Leo](https://resource.dopus.com/user_avatar/resource.dopus.com/leo/32/69732_2.png) [@Leo](https://resource.dopus.com/u/Leo)\
**Post date:** [August 30, 2017, 10:10pm UTC](https://resource.dopus.com/t/script-to-perform-multiple-regular-expressions/3384/12 "2017-08-30T22:10:25Z")

</div>

Thread has been updated for Opus 12, and tidied up a bit.

Things that were covered in replies have been merged into the main post, or included in the new/updated how-to posts it now links to.

---

<div class="post-metadata">

**Author:** ![Leo](https://resource.dopus.com/user_avatar/resource.dopus.com/leo/32/69732_2.png) [@Leo](https://resource.dopus.com/u/Leo)\
**Post date:** [July 17, 2018, 9:02pm UTC](https://resource.dopus.com/t/script-to-perform-multiple-regular-expressions/3384/13 "2018-07-17T21:02:14Z")

</div>

Root post has been updated:

- Now has examples in both JScript and VBScript.
- Extended the scripts to show examples of case-insensitive matching.

(The legacy Opus 11 example has been left as-is, as it's too time consuming to test changes to that, but the same regex etc. techniques should work fine with both 11 and 12. The main difference between the Opus 11 and 12 scripts is how they split the filename stem from its extension, which hasn't changed.)
