Find All Headings Based on a Pattern
Here's a short and easy GREP pattern you can use to find one-line paragraphs
Scott wrote:
Is there a script or GREP code to locate a single line paragraph in a document?
After some back and forth email with Scott, I determined that what he meant was that he wanted to search to find anything that looked like a heading — a single sentence paragraph, but without punctuation. This is an important distinction because GREP cannot find “a single line” — that is, it can’t see where line breaks are, so it can’t see if a sentence fits on one line or breaks across two.
But finding a single sentence paragraph like a heading is not difficult at all, though the code looks weird to the beginner:
^[^.!?]+$
Let’s break this down a bit:
- The caret means “the pattern begins at the beginning of the paragraph.”
- The stuff inside the brackets means that it’s going to be “any one of these” (a choice of any one)
- The caret inside the bracket means “not” — in other words, it’s “not any of these”
- The plus means one or more of the previous thing (“one or more characters that are not a dot, exclamation point, or question mark”)
- And finally the dollar sign means the pattern ends at the end of the paragraph
So it’s any line that takes up a whole paragraph that does not contain one of these punctuation marks. If you want to exclude other characters, just add them to that list inside the brackets.
Scott wrote back and said that someone else gave him this code: ^.{1,20}$ and that it worked okay, too. But that grep code simply means “any paragraph that contains between 1 and 20 characters.” So that might find a short paragraph that you didn’t intend.
Granted, no code will be perfect for all situations. If your headings do, in fact, have puctuation in them, then my code won’t work either. But it’s a start!
This article was last modified on December 21, 2021
This article was first published on December 7, 2012
