Thank you for this idea. It’s what I was looking for. I realize the thread is old-ish, but I expanded the approach to the regex to cover my cases, so figured I would share it in case it’s helpful, or in case anyone can help me improve it.
In particular I wanted to account for content in a block that includes multiple line returns, or the case where the block is at the end of the file without a return. (Basically, it’s about not trusting myself to get the format right all the time.)
/^## #client\/project1(?:[\s\S]*?(?=\n#{1,2} )|[\s\S]*)/
This is where I ended up after a lot of trial and error. So far it’s not thoroughly tested, but seems ok. If anyone finds flaws or can improve on it, please . . .
Here’s what it does:
First match a 2nd level (h2) heading. (That’s just my convention. It could be any level, but needs to be consistent.)
^##
Next, I match a nested tag for client and project.
#client\/project1
Finally, I put the rest inside a non-capturing group to match the balance of the content under this heading.
(?: . . . )
This looks for all content up to another heading of the same (h2) or higher (h1) level, using a lookahead. Or, alternatively, it just goes to the end.
[\s\S]*?(?=\n#{1,2} )|[\s\S]*
The *?
stops at the first match, so that it doesn’t maximize the match, and barrel through to the last h2 in the file.