1

I need to do a find and replace in all cards using the following settings:

As an example I want to find:

<span style="color: rgb(255, 255, 255)">Word</span>

and change it to

Word

In other words I want to strip those text around Word.

but the problem is I have other words too.like:

<span style="color: rgb(255, 255, 255)">One</span>
<span style="color: rgb(255, 255, 255)">Two</span>
<span style="color: rgb(255, 255, 255)">Three</span>

to

One
Two
Three

I want to change them all to the word between those tags. How can I use the search and replace of the Anki to achieve that result by providing a regular expression? What is the correct regular expression to achieve that result?

enter image description here

2
  • What have you tried? I have read that Anki uses the Python flavor of regex, so it should be pretty straightforward. There's even an example in the online Anki manual that I found that is pretty close to what you want. Took less than a minute to find. May 20, 2018 at 20:22
  • Please just provide a sample that works
    – acman123
    May 21, 2018 at 9:33

2 Answers 2

1

The answer seems easy to provide but when a valid regex is applied in the ‘Find and Replace’ it simply does not work.
The documentation https://apps.ankiweb.net/docs/manual.html#find-and-replace is not helpful at all and is even misleading as it shows an example which does not work when applied.
I can only assume that it was working in some of the previous versions but was unintentionally broken.

Let’s look at couple of trials with the ‘Find and Replace’ window. I am using the following settings. enter image description here
Observation 1
Input: <
Find: <
Replace With:
Output: <

Summary: Nothing happened. The pattern is valid but it did not work as expected.

Observation 2
Input: <
Find: ^.
Replace With:
Output: lt;

Summary: lt; reminds html entity &lt; Now we know that the regex engine is working but apparently the characters are stored differently than they look.

Anki stores collections in collection.anki2 file which is an sqlite database file. Let’s open it in DB Browser for SQLite and find the record with:
<span style="color: rgb(255, 255, 255)">One</span>
<span style="color: rgb(255, 255, 255)">Two</span>
<span style="color: rgb(255, 255, 255)">Three</span>

enter image description here

Column sfld:
<span style="color: rgb(255, 255, 255)">One<span><span style="color: rgb(255, 255, 255)">Two<span><span style="color: rgb(255, 255, 255)">Three<span>

Column flds:
<div>&lt;span style="color: rgb(255, 255, 255)"&gt;One&lt;span&gt;</div><div>&lt;span style="color: rgb(255, 255, 255)"&gt;Two&lt;span&gt;</div><div>&lt;span style="color: rgb(255, 255, 255)"&gt;Three&lt;span&gt;</div><div>&lt;span style="color: rgb(255, 255, 255)"&gt;One&lt;span&gt;</div><div>&lt;span style="color: rgb(255, 255, 255)"&gt;Two&lt;span&gt;</div><div>&lt;span style="color: rgb(255, 255, 255)"&gt;Three&lt;span&gt;</div>

Apparently regex works on the flds column.

Let’s try this regex:
enter image description here

Before change:
enter image description here

After change:
enter image description here

The pattern itself can be shortened but here I mainly focused on how to make it work at all.

3
  • Could you please paste those regex you used as copyable text here?
    – acman123
    May 23, 2018 at 0:36
  • Any of those should work: (1) &lt;span style=\"color: rgb\(255, 255, 255\)\"&gt;(.*?)&lt;/span&gt; (2) &lt;span.*?\&gt;(.*?)&lt;/span&gt; (3) (?<=<div>)&lt;span.*?\&gt;(.*?)&lt;/span&gt;(?=</div>) Remeber to put \1 in Replace With.
    – wlod
    May 23, 2018 at 8:00
  • In one of the updates of Anki, the syntax for capture groups was changed. To match the N-th capture group you need to write ${N} instead of \N (e.g. ${1} instead of \1). See this post in the previous Anki forum (archive) for more information.
    – rdrg109
    Sep 19, 2022 at 23:24
0

I thought I would just chime in to say that @wlod's answer is correct.

You can create a capture group by surrounding any matching regex pattern you would like to keep during the replacement by surrounding it with parentheses and using the convention \<digit> ordered by the number of (captured) groups you specified.

This killed me too before I learned of it because in many regex implementations, the $ is used to denote a capture group, but in Anki, the \ is used.


Example #1

You have various animal sentences in a field on your Anki notes. You want to keep the animal and normalize the sentences for all of them.

# Card 1
Your dog is friendly.

# Card 2
That cat is evil.

# Card 3
My fish is dead.

Find:

.*(dog|cat|fish).*

Replace:

I love my \1.

Result:

# Card 1
I love my dog.

# Card 2
I love my cat.

# Card 3
I love my fish.

Example #2

Now let's say you also wanted to keep their describing adjectives. You will need two capture groups. One for the animal and one for the adjective.

You could do this.

# Card 1
Your dog is friendly.

# Card 2
That cat is evil.

# Card 3
My fish is dead.

Find:

.*(dog|cat|fish) is (.+)\.

Replace:

The \2 \1

Result:

# Card 1
The friendly dog

# Card 2
The evil cat

# Card 3
The dead fish

I hope this helps clarify things.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .