Regular expressions, or RegEx, let you find literally anything in your Scrivener projects.
Use Regular Expression or RegEx Searches with Scrivener
Scrivener has two powerful search features. The standard Find/Replace feature allows you to search for text in the file you're working in, and replace it. This feature is similar to what you find in any word processor or text editor. Scrivener also has a powerful Project Search feature, which allows you to search all the elements in your project file: texts, character and setting sketches, research, and anything else in any location in the project.
But there's one more search feature in Scrivener. You can use regular expressions, or RegEx, in either standard Find dialogue or the Project Search field. You may have heard that RegEx is complicated; but in this article, I explain how anyone can use this. Here's how to use RegEx to find anything in your Scrivener projects.
What are regular expressions?
Regular expressions, or RegEx, is one of those things that if you don't know about it, you probably don't need it. Regular expressions date back to the middle of the 20th century, and have been adopted in most operating systems, at one level or another. This tool provides an unmatched level of search granularity.
When you search for a word, using the standard Find dialogue, you type that word, click Find, and the app you're searching in shows you every instance of that word. Regular expressions allow you to search much more precisely. Say you're searching for the word magenta in a text. The standard Find search will highlight all instances of the word, but RegEx searches will allow you to do the following, and much more.
- Find only capitalized or lower case instances of the word.
- Find the word if it is followed by a question mark or exclamation point.
- Find the word if it is anywhere inside quotes.
It may seem to be an exaggeration, but you can literally find anything with regular expressions. And before you think this is too complex, read on to find out how easy it can be.
Using regular expressions in Scrivener searches
Default searches in Scrivener do not use regular expressions, but it's easy to switch this on. In the standard Find dialogue, click the Find Options menu and choose Regular Expression (RegEx).
In the Project Search field, click the down pointing caret next to the magnifying glass, and chose RegEx.
Just enter a regular expression, then click Next in the Find dialog, or press Enter to perform a project search.
Scrivener uses the stock RegEx engine supplied by the Mac, which uses the UTF-8 compatible ICU guidelines. ICU is mostly compatible with PCRE, which is considered to be the standard for extended regular expression syntax. On Windows, Scrivener uses the QT5 Regular Expression engine, and is fully PCRE compliant.
Examples of regular expressions
Regular expressions can be very complex, but, as you will see below, there is a way to make this very simple. First, here are some examples of regular expression
- To find only capitalized instances of the word "magenta":
- To find only lower case instances of the word "magenta":
- To find the word magenta in upper or lower case:
- To find the word "magenta" followed by a question mark:
- To find the word "magenta" anywhere inside quotes:
These examples are all pretty simple, but in the last one, you see the use of the * wildcard, which means any number of any characters. Here are a few complex examples of RegEx:
- To find any URL:
- To find any email address:
- To find any date, in the format "dd-mm-yyyy" for years between 1900 and 2099:
As you can see, regular expressions can find anything.
Here's one useful example that you may want to use when revising a novel. To find all instances of repeated words, where you accidentally typed "the the," but also where you correctly typed "had had," use the following:
\b: Indicates a word boundary.
(\w+): Matches one or more word characters (a-z, A-Z, 0-9 and _). The parentheses define a capturing group (functioning like a variable), for the repeated word.
\s+: Matches one or more whitespace characters.
\1: Is a backreference to the group (
\w+), matching the exact same word.
\b: Indicates a word boundary.
That's pretty complicated, right? I told you that there was an easy way to do this, and it involves leveraging ChatGPT. In Should Writers Use AI Tools?, we looked to how writers can use tools like ChatGPT to generate ideas. This sort of AI tool may not be good at writing fiction, but it happens to be extremely good at writing computer code. If you're a developer, you can ask it to write code for you to perform certain actions, and you can also ask it to give you regular expressions.
For years, developers and others using RegEx would refer to weighty tomes, or visit websites with examples, but that still requires a knowledge of this syntax. With ChatGPT, you can enter a text string in normal human language, and get a result.
For the above example, I asked ChatGPT to "Give me a regular expression to find repeated words." In seconds, it gave me the results, and explained them. The result was the following: `
\b(\w+)\s+\1\b ` In order to use this in Scrivener, remove the backticks at the beginning and end of the string, then, after choosing RegEx in search options, paste that string and perform the search.
Here's one more example. I asked ChatGPT to, "Give me a regular expression to find text that has an open quote but no closing quote." It returned the following:
": Matches the start quote.
.*: Matches any characters (except a newline) 0 or more times.
[^"]: Matches any character that is not a quote.
$: Asserts position at the end of a line.
Regular expressions allow you to find anything in your Scrivener project. You can adapt them to your characters and settings, to find specific instances of text in the largest project. While you may not use RegEx often, they can be a time-saver, especially when revising or editing a long text.