Help wanted with RegEx removing empty lines above and beneath images

image

This RegEx finds the images and the captions. I want to remove the empty lines above and below them.
When I add \n before and after the Capturing group, Scrivener won’t find them any more. When I enter them using Ctrl+Enter as the Tooltip suggest… Scrivener won’t find them any more either.
I’ve tried the multiline mode (?m) , the global mode (?g) and the single line mode (?s) before the RegEx without success.

Does anybody knows how to do this on a Windows machine?

You can remove all empty lines (and you probably should), but I doubt RegEx can detect images.

It is detecting the text that’s replaced with images. As you can see in the image by the yellow background of that text.
So how do I remove all empty lines?

Images are not text, so I don’t see how that does anything about empty lines around embedded images. If they were not embedded and instead you used placeholder tags such as <$img:image name>, you can work up a RegEx to match that. In that case jpg, jpeg, or png is not part of the name, so you’d key on $img itself.

Removing empty lines is simple, just replace ¶¶ with ¶ in Edit/Replace or Project Replace.

Ah. Now I realize you’re replacing them with a Markdown syntax.

That works. Except. I don’t want to remove ALL empty lines. Only the ones around the text replaced by images.

In that case, I’d have to puzzle out what’s wrong with your RegEx. Do you want to do this in the Editor or Compile?

Do you need to do \r\n for Windows to match the Windows CRLF?

Tried that and [\r|\n|\r\n]…

I’d like to replace empty lines around text to be replaced by images in the Editor…

There were some regex bugs during the beta. Might be worth a quick search to see if you are running into one that isn’t fixed yet.

Here’s a string that matches in the project search box, but doesn’t match in Find and does match in a Compile Replacement.

\n!jpg(.+)\n!fig(.+)\n\n

This is on a Mac.

1 Like

That’s on a Mac, by the way.

Needing a capture Group for replacement that would be:

\n(!jpg(? :.+)\n!fig(? :.+)\n)\n

  • Created a capturing group around the img and fig
  • Changed two capturing groups to non-capturing groups

Replace with $1

Daunting action to attempt in Project Replace: no chance to test, or anything…

You can test it, the same way I tested it, and you’ll have to, to figure it out.

I’ll have to test the replacement as well. :wink: I think I will start a test project for this.

Did so:

No luck. I think this is broken. :sleepy:

The pattern (mine) worked in search but not in Find. I do think that’s a bug.
Replacement worked in Compile – but I didn’t try returning the captures (if I captured them at all).

I just realized, according to the screenshot, you have “Contains” selected, not RegEx.

Check. I go try. Again.

Well, I got it to find the text with the Returns around it.
I even got it to replace the found text with something.
Unfortunately, the only text it will replace it with is “$1”, literally.
Not the content of the Capturing Group $1.

Broken differently?