Regular expresson anchor discrepancy [Fixed in v5.4.3]

I had a draft with several lines of text that had been pasted in from another source. Many of the lines had trailing spaces (one or more), and I wanted to delete them via find-and-replace. The straightforward regular expressions to do this,

Find: (.+)\s*$
Replace: $1

found only the last line in the draft. And if I altered the Find regex to

^(.+)\s*$

it wouldn’t find anything at all.

After some experimentation, I learned that the anchors ^ and $ refer to the beginning and end of the draft as a whole, and not to the beginning and end of each line in the draft. This contradicts the NSRegularExpression documentation that Greg links to on the Drafts Find and Replace page.

In fact, the example on the Find and Replace page—where first and last names are reversed—doesn’t work as described. It reverses only the names on the first line of the draft, not all of the lines.

Am I missing some regex flag that needs to be set to get the anchors to work on a line-by-line basis, or is the documentation out of sync with the current implementation?

Your original expression seemed to pick out multiple lines for me in a quick test case draft.

There is an “m” flag for NSRegularExpression, but I assume that’s enabled by Drafts (Ref. NSRegularExpression - Foundation | Apple Developer Documentation.

Thanks for posting a screenshot. I noticed you had the “Ignore case” option turned on, and that made the difference.

Ignore case off finds just the one at the end:

Ignore case on finds all of them:

This makes no sense to me, but at least I now have a workaround.

1 Like

Okay, same result around the ignore case option for me on the latest beta version (5.4.3.8). I agree, that does seem rather peculiar :confused:

@agiletortoise - something by design or a bug?

Found this. It will be fixed in 5.4.3. The anchors match lines options was not getting set unless ignore case was enabled.

2 Likes