Editor: batch replace in one field, also with regex [Official thread]

If you have a problem with the add-on editor batch multi replace post in this thread. This way I get notified. If you make a new thread I will likely miss it.

2 Likes

How about changing multiple things in one field in many notes ?

You’re looking for “Find and Replace…”, which is a built-in Anki feature, which you can access from the browser.

2 Likes

Thanks, but how can i add many replacements

You must run it multiple times.

Great!
It would be good to know in advance that it uses rather Python-compatible than POSIX-compatible regex, so eg. one have to use rather \1 than $1 for backreferences. And that it operates rather on content that you see in HTML editor than on content you see normally in editor while adding/editing a note. It took me a while to figure it out.

1 Like

@JBrown: Thanks for reporting the error. Because of my custom config I didn’t notice that for other people my add-on had a bug. I just uploaded a new version that should fix it. Could you update your add-ons, restart Anki and try again. If you find other errors please let me know.

@ijgnd It would be possible to use your add-on to capitalize the first letter of a field?

I’ve tried using this notepad++ - Regex: Convert the first lowercase letter of the starting words into capital Letter (after .dot) - Super User but it break the add-on.

@gustavosmen: there’s probably some way but it’s too complicated for me … So I can’t help.

the relevant part of the source code is in the file editor.py of my add-on in the function batch_replacer_helper (the line html = re.sub ...)

The problem is that I edit the html source code. So the first visible character is not necessarily the first character of the html source code. So I would have to handle all special cases that could occur or work back from plain text to the html source …

I have no quick fix or code snippet for this.

1 Like

Hi man
So I posted on reddit the following : I want an add on that removes the extra spaces between words especially when copying and creating flashcards from a PDF. and one person recommended your add on but he said put
in the add on config and I tried it and it didn’t work probably because I use the code in a wrong way so could you help me ?

@Yaser: It depends on how the html source code looks like. I don’t have some snippets that work in each case. But I plan to update the remove linebreak add-on soon (once the next anki version is released which has support for an essential add-on for me).

@ijgnd Hi, the add-on breaks in the new browser’s editor. It doesn’t do the regex properly.

for me it works in Anki 2.1.54. can you share the regex and the expected result. Have you confirmed that your regex expression works as expected in a different tool like an online site like regex101.com (with the python setting)? What OS do you use. what exact anki version? does this still happen after having disabled all other add-ons and having restarted anki?

Hi, i meant to say in the new version: 2.1.55, with the browser in vertical mode

it also works in 2.1.55 in the browser with each editor orientation for me. without more info there’s nothing I can do (see my prior post).

Thanks a lot for writing the add-on! It’s extremely helpful!

Here’s what I’m wondering: In the add-on description on AnkiWeb it says that the add-on “gives you a shortcut to apply all of these substitutions/replacements on the currently active field of the editor”. However, when I press the shortcut, the config window opens. The same happens when I click the context menu entry. So the only way I found to invoke the substitutions is pressing the toolbar button.

Am I doing something wrong or is that the intended behaviour?

Thanks for reporting this bug. I’ll upload a fixed version when I have more time.

If you avoid shortcuts with shift it should work as expected. I never noticed this as a problem because the shortcut I use “Meta+ctrl+e” works.

You’re right, when I set the shortcut to something without shift it works indeed! Thank you for the tip!

The Windows key functions as Meta key, right? Because I just tried out what happens when I set the shortcut to “Meta+Ctrl+E” as you suggested but nothing happens when I press “Windows+Ctrl+E”.

I have a suggestion and a question. :blush:

My suggestion:
Add the following text to the toooltip when the user hovers over the toolbar button:

Hold “Shift” and click the button to open the config window for simple replacements.
Hold “Ctrl+Shift” and click the button to open the config window for regex replacements.

My question:
Are the simple replacements and the regex replacements always both carried out? Or is it somehow possible to invoke one without the other?

at the moment the regular and regex are always both run. to change this you’d have to edit the source code of the add-on. I’ve added it to my todo list but at best it will take some months …