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.
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.
dataview usage is quite underdocumented, isn’t it?
the following dataviewjs function can show all filenames, and instances that match a regex pattern.
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("/", ".*@.*");
thank you so much! i appreciate you helping as i needed to use dataview regex matching for a lot of situations
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.
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
about
this is a project
introduction
this is the intro
ending
this is the ending
introduction
this is another introduction
This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.