Regex that finds and replaces within YAML frontmatter? AI can't solve it, been haunting me for days

I cannot, for the life of me, 1) understand any tutorial or blog or Reddit post or Stack Overflow dialog.

I need a regular expression that matches only wikipedia-url and amazon-url

It can’t match below the frontmatter.

This has stumped every AI, across every implementation.

---
wikipedia-url: https://en.wikipedia.org/wiki/SOLID
amazon-url: https://www.amazon.com/What-Technology-Wants-Kevin-Kelly/dp/0143120174
author: "[[Kevin Kelly]]"
---
created-by: "[[Obsidian.md]]"
github-url: https://github.com/obsidianmd/jsoncanvas

If you can answer it and it works, I will Venmo you $200

See:

with key and values:

BTW, you are using wrong (smart) quotes. You need dumb quotes around wikilinks!

2 Likes

You’re not really saying what you actually want to match towards. Within Obsidian you should be able to use something like line searches if you aim to just verify the links. This could theoretically match a line outside of the front if it follows the same syntax.

If you want to verify the properties themselves I’d strongly consider using Dataview, and using matches against the specific properties.

Lastly, the quotes are OK, @Yurcee, but since the OP didn’t enclose the markdown in a code block the forum software transformed them into smart quotes. This is one of the reasons it’s always recommended to enclose markdown in code blocks with a line in front and after with four backticks:

````
author: "[[Kevin Kelly]]"
````
1 Like

How many instances of amazon-url and wikipedia-url do you have outside of properties? It may be easier to temporarily change them to something else so you can run a regex that doesn’t try to match only within properties, like

^(amazon-url|wikipedia-url)(?=:)

(which matches either of the strings if it begins at the start of a line and is followed by a colon).

Otherwise I think you’ll be trying to do a multiline lookahead and a multiline lookbehind, which besides being potentially tricky to figure out (certainly painful to read) may end up freezing Obsidian.

Also, I edited the original post to mark the code as code.

2 Likes

Not sure what exactly you want to replace:

  • Do you want to rename properties, eg wikipedia-url to www-wikipedia?
  • Do you want to replace the content of a property, eg. replace http: with https: for all Wikipedia URLs?

And with what tool do you want to perform this replace? Obsidian on its own doesn’t do regex. Do you have a particular plugin in mind? Or an external editor like VS Code? Or are you writing a script?