Obsidian query capture text between two headings with regex

Hi all,
I’d like to use Obsidian’s query feature to embed all text below certain headings.
For example, I have lots of headings that start with a given date and have some text below it which is then followed by another heading. It’s that text I’d like to query together with its heading:

#### 2023-08-10
some 
nice text
#### 2023-08-15
some more text

My regex query is

content: /#### 2023-08-28([\s\S])([\s\S]*?)(?=####)/

Which returns:

#### 2023-08-10
some 
nice text
#### 2023-08-15

How do I need to modify the regex query so that the second heading #### 2023-08-15 is not returned?
According to the regex testing site https://regexr.com/ it should work.

Thanks for the help,
m

1 Like

Are you on mobile? Positive lookaheads (or any other lookies) might not be supported there.

I’m on Windows 11, Obsidian 1.4.5.

Out of curiosity, why are positive lookups not supported on mobile?

iOS limitation I think (not sure about Android – don’t use them anymore).
But now that I come to think of it, Obsidian should and will throw an error anyway.

I don’t know the reason, but they weren’t supported on iOS until recently (they are in recent versions).

Having played around with it a bit more, I noticed that when using the regex search in the regular Obsidian search field it highlights the correct, but it still displays the next line:
image

Maybe this is intended behavior or a bug?

I use an empty line between headings. Then it’s better. ‘Toggle Show more context’ off and on as @CawlinTeffid suggested.

These changes are easily done with one of the Linter community plugin’s settings – one can make changes on individual files and whole folders (even the complete vault) with it.

I tried toggling context, didn’t make a difference.

I tried your suggestion:
When adding an empty line, I get the same result.
image

However, I can change the query to look ahead for an empty line: /#### 2023-08-28([\s\S])([\s\S]*?)(?=^\s*$)/
In this case the next heading is not returned and also not the empty line. This is inconsistent with the result of the previous query, though. I’d expect to also have the empty line returned. It’s not clear to me why that would be.
image

I guess, I can adapt my workflow accordingly to incorporate this, though :slight_smile:

Okay, so I found a solution for my specific usecase:
/^####\s2023-09-04(.*)$\n([\s\S])([\s\S]*?)(?=$\n#{1,4}(\s)*)/

where
^####\s2023-09-04(.*)$\n matches my datestamped heading and any text that may be present on the same row.
([\s\S])([\s\S]*?) matches the text below the heading until the next heading appears (?=$\n#{1,4}(\s)*)

Maybe this will help someone in the future.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.