Skip to content

Instantly share code, notes, and snippets.

@snoyes
Last active September 10, 2023 14:20
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save snoyes/405b1e0615113a393ab7bd0d1be9b2d0 to your computer and use it in GitHub Desktop.
Save snoyes/405b1e0615113a393ab7bd0d1be9b2d0 to your computer and use it in GitHub Desktop.
Anki: move part of field to different field, or remove unwanted content from a field
# Open the debug console with Ctrl+: (which is Ctrl+Shift+; on US keyboards)
# (Don't have the browser window open when you do.)
# Run it with Ctrl+Enter
# Remove the # from the last line to actually save the changes
s = r'regular expression to search for goes here'
srcField = 'Source Field Name goes here'
tgtField = 'Target Field Name goes here'
noteType = 'Name of the Note Type goes here'
# Get the model, and then the list of notes of that type
theModel = mw.col.models.byName(noteType)
theNotes = mw.col.models.nids(theModel)
# Run through the list of notes
for nid in theNotes:
# Get the note contents
note = mw.col.getNote(nid)
# Search the source field for the regexp
m = re.search(s, note[srcField])
# If there's a match...
if (m):
#... show what it found
print(m.group(0))
#... add it to the target field (comment out if just clearing unwanted content)...
note[tgtField] = m.group(0)
#... and remove it from the source field
note[srcField] = re.sub(s, '', note[srcField])
# save the changes
#note.flush()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment