Search by tag, exact match (excluding subtags)

What I’m trying to do

I’d like to search all notes that matches exactly with a given tag (and ignores any subtags).

For example:

  • Notes:
    • Note1 (#action)
    • Note2 (#action/p1)
  • Goal:
    • Search all notes with the tag #action but not #action/p1, etc.
  • Expected Result:
    • Note1

Things I have tried

Things I have tried:

  1. Click action tag in tag pane: returns Note1, Note2
  2. tag: action: same as 1)
  3. tag: #action: same as 1)
  4. tag: "action": empty results
  5. Right-click on action in tag pane, and no option to search with exact match.

Works but flawed:

  1. Regex /^#action$/: Searches notes that has text #action in codeblock

Solution Ideas

If there aren’t any that work:

  1. tag: "action" or tag: action$ for exact match (some special characters)
  2. tagregex: /^action$/ (regex-version of tag)
  3. Path-like search in tags (didn’t think about backward compatibility but you get the idea…)
    • tag: action → exact match
    • tag: action/* → all notes with direct subtags of action
    • tag: action/**/* → all notes with all subtags of action
    • tag: action/**/* -tag: action → All notes only with subtags of action
  4. Right-click on a tag in a tag pane and there is search for #action exact match

Thinking further, the most flexible way would be introducing tagregex or making tag accept regex + hiding its complexity behind context menu for exact search.

Suggestion for Doc:

  • Working with Tags says clicking a tag returns an exact match, but in the app, it also returns all the notes with the tag’s subtags, which in my opinion is not an exact match.
  • Why does the forum thread template show “Things I have tried” first and “What I’m trying to do” later? It seems natural to reverse the order.

Thanks!

I just found this post, who also suffers from the lack of a way to search by tag (exact match). And I like the suggestion there, too (except that it changes UX) I’d love to see a discussion on providing a way to do exact match tag search, without any side-effects (unlike general search using regex).

There exists a not-so-ideal workaround using the Dataview plugin.

list from #tag
where contains(file.tags, "#tag/") = false

If subtags are always separated by forward slashes, main tags can be isolated by negating the forward slash with regex. The following works in my tests of your samples to only find #action and to ignore #action/p1:

/#action[^\/]/

Works for you?

Angel

Thanks. But I feel that having to use a community plugin for this is an overstretch.

Thanks. It looks similar to one of “things I’ve tried”. It treats the entire text holistically and finds things that aren’t actually a tag i.e. text in a code-block, no?

Apologies. I misunderstood.

Angel

1 Like

For me, your /^#action$/ doesn’t find the pattern if it is inside a codeblock. See file attached.

Am I doing something wrong / different?

Angel

action.md.zip (334 Bytes)

No worries, you are trying to help. I thank you!

Nice, you discovered a good case that my regex can’t find: which is a tag immediately followed by a disallowed character in tag like ,)`: #action,#keyword(#today) or any inline codeblock, which explains for your 4 bullet points.

$ marks an end of a word, which is an encounter with whitespace or end of the file.

What I’m puzzled about is your Obsidian not finding #action in non-inline codeblock. Mine finds it:

I’m not sure what the difference is. Maybe can you show mark-down text?

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