Correct Replace Syntax

I’m trying to de-LaTeX a document that Pandoc choked on. The document is now in Scrivener. I’m trying (at least at this stage) to replace \section with # for easy conversion to Markdown. However, when I try to convert using Project Replace, I get the following ‘not found’ error:

Find: #{
Replace with: #

So Scrivener isn’t seeing the bracket–or maybe the crosshatch as well.

Another somewhat related issue: I need to remove all of LaTeX’s index entries. These are in the format \index{Labor Law!Employment Contract!writing}. The text in the brackets changes based on the index entry. I would like to remove all of these entries–they are not on separate lines, either. Maybe there’s an awk or sed way of doing this, if I can’t do it from within Scrivener.

The substitution works using ctrl+f, but not Project Replace. Odd.

If you’re using RegEx, you have to escape the bracket and search for \#\{ or #\{. Or do an Any Word search for #{.

1 Like

Yes, curly-brackets are special characters in regex syntax, used as quantifiers. The pattern, if properly formed, would be something like #{1,6} where that would mean “look for one to six hashmarks in a row”. #{ is meaningless, a syntax error.

Which is why I wanted to change the Pandoc-generated


to “plain” Markdown:


A crosshatch alone won’t display here.

As the forum uses Markdown itself, yes you do need to escape special characters with a \ character:


Sounds good though, you just need the pattern to be valid, which @drmajorbob describes how to do. \{ tells regex to look for that symbol itself, not use it as syntax.

That said, I see a bug in Project Replace where if you use regex mode and select fields in the lower part that might be empty, it throws an internal error that you won’t see unless you have internal error alerts enabled. It should be skipping over empty fields (like custom metadata or synopsis) rather than passing empty data to the regex engine, it looks like. If you don’t see error alerts, then it will just silently fail, and lead you to believe there is a problem with the search parameters, when in fact it would fail no matter what you type into the replace/with fields.

Assuming what you’re really looking for is #{Name of Thing} and want that turned into # Name of Thing, then this pattern works:

Replace: ^#\{([^}]+)\}
With: # $1

(If you’re wondering, you often do not have to escape special characters inside of a range: [^}]+ means one or more characters that aren’t “}” specifically.)

# :thinking:

I guess you’ve to escape it with a “\” in front, like \#

In my reply up above, I had no problem entering # and { without \.

(and again here)

I did have to escape \ itself.

From a purely forensic point of view, it is interesting how in the digital world things that can’t happen do happen and no one really knows why.

Well, in this case it is not so mysterious. You usually only need to escape characters that mean something in Markdown. #24 is not Markdown, nor is talking about the character ("#") in passing.

But put it at the beginning of a line? Well now, that…

Means Something

The same is often true elsewhere, like in our regex example above. A curly-bracket after a character like that is most definitely syntax (incomplete in this case). But this same character inside of a [^...] range is just a character, because the interior of a range uses a different and much simpler syntax than regex.

When in doubt, escape. Most systems will not choke on over-use of escaping. You can’t see it here, but this: # is escaped, even though there is no need to do so.

1 Like