Search and Replace with Regex [SOLVED]

I want to do a Search and Replace with Regex:

replace all instances in all files in my vault of == by **.

What search syntax would I have to put for Search and what syntax for Replace?

I had posted a previous reply saying that you don’t need regex for that, but I think I understand your need now. You want to find two and only two consecutive equal signs, right?

If so, this should work:

Find: ([^=])==([^=])
Replace: $1**$2

I’m not near my computer right now, so I can’t test this in Obsidian. Its regex engine may use backslashes instead of dollar signs for group reference.

1 Like

@macedotavares: I thought you deleted your post because you got annoyed with my simple question. I subsequently deleted my post because I agree with you that you don’t need Regex for it.

That is correct because I want to replace all highlights with bold. After reading your initial comment and thinking about it, I thought I could do:
Find: ==
Replace: **

I checked it with VS Code (have not executed it yet) and it seems to find all instances.

Do you agree that my simple thinking is right, or is it still better to use your Regex expression?

You should use regex. Otherwise it will replace any sequence of an even number of equal signs. It may mess something up.

I think VS Code uses backslashes. Please test that and let me know. Cheers!

I have done this:
Find: \([^=])==([^=])

but it does not find anything.

The backslash at the beginning is escaping the first (, so it’s looking for the actual character. You don’t need it.

The backslashes I was referring to (and which may have caused the confusion) are those in the replacement expression. Some regex engines use $1, $2, etc. and some use \1, \2 etc.

Please try again and let me know how it went.

Even with ([^=])==([^=]) it does not find anything.

Well, I’m stumped.

Just one final check, if you’re using VS Code: is the tiny regex toggle (in the right side of the search box) activated? I sometimes forget about it and vscode understandably just does a literal search for my expression.

You can also check out this regex101 saved search and do some experiments:

Keep me posted. We’ll figure this out.

Ah yes, that was it, it was not enabled. Yes, now it finds the files.

It found many more instances tahn when I just did == for find. I have not gone through all the results, but I found one where it identified the ==, even added ** in the pane showing the effect, but it does not show it to change.

For most/all others it shows:

Might be to do with the comma after “mistake”?

The [^=] bits should match anything but an equal sign and the $1 and $2 should replace it with whichever character that happens to be.

I sometimes have a hard time interpreting VS Code’s replacement preview. Maybe it will perform fine when doing the actual find/replace. If it doesn’t, let me know; I’d be curious about why it doesn’t work.

It did a number of them, but not all. Here is an example:

This one is similar but partially done:

When I do my simplistic search, i.e. ==, it finds almost half as many again as the Regex expression, incl. the ones above.

:face_with_monocle: I can’t think of any reason for that difference. I’ll do some testing on my Mac later and get back to you.

Please don’t do too much, you undoubtedly have more important matters to attend to.

So, after I ran the S & R on VSC with your Regex, I came across instances where the == had not been replaced. I just came across this:

It actually added **. So, I’ll run my simplistic S&R and see what happens. If there are still == remaining I’ll ask for help again.

Yeah, I forgot about those cases where there’s nothing before or after the ==. Sorry about that, and thanks for the update.

Edit 2020-10-12

I’ve tried a different approach and this seems to work rather well in VS Code:
Find: ==([^=].+?[^=])==
Replace: **$1**

Just one stupid question:

are you talking about the Obsidian built-in replace function, which is located in the editor and only works for a single file?

I thing that replace function does not support Regex

@GLight: it’s not a stupid question, you’re just asking for clarification :grinning:

The question was for a global search and replace. I am aware of the fact that Obs does not have the Global Replace bit, which is why I had to do it through a 3rd party, in this case I used VS Code.

For the sake of completeness I can report that my simplistic, non-Regex way worked, although I had to run the function twice, for some unknown reason.

Thanks for asking the question because you made me realise this thread had not fully been fully completed.

It’s really a nice question, since it made me realize my mistake. Obsidian doesn’t support global regex find and replace, so my answer was misleading. I always use VS Code for that.

@GLight @Klaas There is a feature request for global search and replace.

@sam.baron: cheers, I was aware of that one and had already hearted it. While we await possible implementation we need a workaround.