# Dataview files match regex and show output

**URL:** <https://forum.obsidian.md/t/dataview-files-match-regex-and-show-output/76342>\
**Category:** Help\
**Tags:** dataview\
**Created:** [February 5, 2024, 6:50am UTC](https://forum.obsidian.md/t/dataview-files-match-regex-and-show-output/76342 "2024-02-05T06:50:29Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![lsll5](https://forum.obsidian.md/letter_avatar_proxy/v4/letter/l/74df32/32.png) [@lsll5](https://forum.obsidian.md/u/lsll5)\
**Post date:** [February 5, 2024, 6:50am UTC](https://forum.obsidian.md/t/dataview-files-match-regex-and-show-output/76342/1 "2024-02-05T06:50:29Z")

</div>

dataview is useful for showing things in the vault using queries, but matching regex patterns is very important and useful for many situations.

i’ve searched everywhere but can’t find a way.

---

<div class="post-metadata">

**Author:** ![constructivecritic](https://forum.obsidian.md/letter_avatar_proxy/v4/letter/c/ebca7d/32.png) [@constructivecritic](https://forum.obsidian.md/u/constructivecritic)\
**Post date:** [February 5, 2024, 6:58am UTC](https://forum.obsidian.md/t/dataview-files-match-regex-and-show-output/76342/2 "2024-02-05T06:58:05Z")

</div>

dataview usage is quite underdocumented, isn’t it?

the following dataviewjs function can show all filenames, and instances that match a regex pattern.

```plaintext
async function fetchregex(directory,regexstring) {
  const regex = new RegExp(regexstring, 's');
  for (const p of dv.pages(`"${directory}"`).sort(x => x.file.name, 'desc')) {
    const f = app.vault.getAbstractFileByPath(p.file.path)
    const c = await app.vault.read(f)
    const s = c.match(regex);
      if (s) {
        dv.header(3, `[[${f.basename}]]`);
        dv.paragraph(s);
      }
    }
}

// this calls the function to fetch all emails from your vault
fetchregex("/", ".*@.*");

```

---

<div class="post-metadata">

**Author:** ![lsll5](https://forum.obsidian.md/letter_avatar_proxy/v4/letter/l/74df32/32.png) [@lsll5](https://forum.obsidian.md/u/lsll5)\
**Post date:** [February 5, 2024, 7:00am UTC](https://forum.obsidian.md/t/dataview-files-match-regex-and-show-output/76342/3 "2024-02-05T07:00:43Z")

</div>

thank you so much! i appreciate you helping as i needed to use dataview regex matching for a lot of situations

---

<div class="post-metadata">

**Author:** ![lsll5](https://forum.obsidian.md/letter_avatar_proxy/v4/letter/l/74df32/32.png) [@lsll5](https://forum.obsidian.md/u/lsll5)\
**Post date:** [February 12, 2024, 6:02am UTC](https://forum.obsidian.md/t/dataview-files-match-regex-and-show-output/76342/4 "2024-02-12T06:02:25Z")

</div>

i did a TON of testing 😭 and i should share improvements i have made to this dataviewjs query so that it would work for stopping the match when encountering a blank line.

i added a few regex flags.

```auto
async function fetchregex(directory,regexstring) {
  // regex constructor, documentation at https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/RegExp
  // use the flags "i" for case insensitive, "m" for (multiline ^ and $ march on each line), "g" for global matching for matching all instances, "s" for making the dot matching new lines
  const regex = new RegExp(regexstring, 'imgs');
  // "s" allows the `.` to match newlines, "g" matches all instead of stopping at the first match, "i" ignores letter casing differences, "m" to treat beginning and end assertions (`^` and `$`) as working over multiple lines
  for (const p of dv.pages(`"${directory}"`).sort(x => x.file.name, 'desc')) {
    const f = app.vault.getAbstractFileByPath(p.file.path)
    const c = await app.vault.read(f)
    const s = c.match(regex);
      if (s) {
        dv.header(3, `[[${f.basename}]]`);
        dv.paragraph(s);
      }
    }
}

fetchregex("/","introduction(?:^$|\Z)")

```

this way, all the occurrences that start with “introduction” matches until an empty line

```auto
about
this is a project

introduction
this is the intro

ending
this is the ending

introduction
this is another introduction

```

---

<div class="post-metadata">

**Author:** ![system](https://forum.obsidian.md/uploads/default/original/3X/b/e/bec35fbd23ab28909bb6bfe62cb32eb82dd34019.png) [@system](https://forum.obsidian.md/u/system)\
**Post date:** [February 19, 2024, 6:02am UTC](https://forum.obsidian.md/t/dataview-files-match-regex-and-show-output/76342/5 "2024-02-19T06:02:37Z")

</div>

Closed on February 19, 2024, 6:02am UTC

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