Use of regular expressions in Compile Replacements

Hi Scrivener team,

I’d like to suggest for your consideration that regular expressions might be used in the Replacements Compile settings as well as in the search box.

I use some sort of my own in-line keywords to help me tag character dialogs, like in:

“That might become handy one day”, -C- she said, “let’s grab it.”

Where -C- is my keyword for a line said by character C.

These have proven useful for tracking each character line while not splitting the document too much as if I had used a real Scrivener keyword. I have similar in-line keywords to track mood, tension and humor bits.

Then I set up the compile settings to replace -C- by the null string (which, BTW, only works in the Mac version, the Windows version does not seem to like the null string and does not replace anything).

The catch with this method is that I have to define a replacement for every keyword of my own, and I have to do it for every project.

If regexps were allowed at compile stage, for instance, I could simply define one replacement like -[A-Z]- instead, allowing for much more flexibility.

What do you think?

Enabling RegEx search in the various find/replace fields and the compile replacements is already on our list for Windows and exists for Mac on newer versions of the OS (it depends on code that was made available in 10.7). :slight_smile: On the Mac version, take a look at section 21.1.5 in the user manual for more info.

The empty replacements string problem on Windows is a bug and is already on our list to fix.

Just to throw another option out there, though you may have already rejected this for other reasons, inline annotations are handy for this sort of paragraph tagging that you’re doing, since you can search for them, even limiting the search to text within annotations using Find by Formatting, their colouring helps them stand out, and you can easily strip annotations (separately from comments) when you compile. That’s an option that works for both Mac and Windows.

This is something that is definitely coming in a future version. It was introduced to the Mac version very recently, in fact:

So it will be coming to the Windows version, too.

All the best,

EDIT: Ninja’d by Jennifer.

Just curious — which dialect of REGEX will be supported? See Different Regular Expression Engines.

I’m running the Windows version of Scrivener ( so my PDF User Guide contains no information about Scrivener REGEX for the Mac. My concern is that the Mac and Windows/DOTNET implementations will behave similarly enough that we — as in all of us, developers and users — don’t end up going down a “REGEX for Mac version” versus “REGEX for Windows version” rabbit hole…

Cheers & hope this helps,

Yes, MimeticMouton, you’re right. I used annotations at first and I still do, for other purposes. I also tried to use colours to tag some text according to what I needed.

But I ultimately resorted to these in-line keywords because they allow me to search and create dynamic collections without the need to use the Find command. I can do all things that you typically do with regular keywords, like show documents with dialogues between characters A and B, etc., which stand out gracefully too at plain sight.

AFAIK it is not possible to create similar collections with annotations, comments or footnotes, or is it?

Oh, this is really great! Thanks for the info!

[size=150]Coming soon to a Scrivener near you[/size] :laughing:

I’m not sure I follow. I meant that you would use your keyword exactly as you do now, -C- or such, but place that within an inline annotation. It is treated exactly like other document text when using the project search, so you can build collections with it and so on as you were doing; it just has the additional advantage of being able to further refine a search using Find by Formatting and, key for what you were initially asking about, being removable during compile just by ticking an option.

Oh, I see what you mean now. Yes, you’re right again. When I first started using those in-line keywords I marked them as annotations, but It was not as straightforward to do so since I had to select the keyword and press Crtl-Shift-A every time.

I got used to type a simple -C- string.

Then I couldn’t find a way to do a massive search for a plain text string and replace it with a formatted text, in order “convert” all -C- instances to annotations. Do you know if there is a way to do something like that?

Not in one shot, no. You’d have to load all the relevant documents in a Scrivenings session, then use Find to go through each instance. The relevant phrase will be pre-selected, so when you switch focus to the editor you can use the annotation shortcut to apply that, then go to the next instance, repeat, etc.