At the moment, the syntax for searching cards has quite a few inconsistencies and undocumented behaviour.
Examples
- Single
\
matches nothing but doesn’t throw an error, either. -
\\*
matches literal\*
(instead of\
and then anything). -
%
is treated like a wildcard. -
(
can be matched anywhere, but)
only in quotes. - If unquoted,
\"text\"
throws an error. -
a"b"
,"a""b"
,"a"(b)
,(a)(b)
are all valid, buta(b)
is not. -
tag:"a c"
doesn’t match anything, buttag:a_c
andtag:"a c*"
(apart from matching the expected) match cards with tagsa
andc
unless they have a tagb
. -
:
can’t be matched, either. A funny workaround is*:*:*
.
I would like to redress these issues but for this there needs to exist a fixed and consistent syntax without ambiguities. To achieve that, I want to put forward the following list of clarifications and changes (numbers only for ease of reference):
-
\
is always an escape character. An undefined escape sequence is an error. - The defined escape sequences are:
\\
,\"
,\:
,\*
,\_
,\(
,\)
. They are required to match the corresponding special character. -
%
is not a special character. - All of the escape sequences might be used outside quotes.
- Escaping
(
and)
inside quotes is optional. - Escaping
:
is optional if it is preceded (not necessarily immediately) by another unescaped:
in the containing string. - Text preceded by certain keywords (
re:
) might deviate from all of the above, but escaping"
is obligatory. Ergo, quoted text is always terminated by a"
preceded by an even number (including 0) of\
s. -
tag:
doesn’t work across tag boundaries (spaces). - A string starting with
:
is an error because the empty string can neither be a keyword nor a field name.
What do you think? Will implementing this will break anything (that is not broken, yet)? Or have I missed something that also should be included in this list? All feedback is welcome.
By the way, I’ve tried to be as permissive as possible because I know some users may depend on the status quo. But personally, I think Anki would benefit from a stricter rule set (in the search context and in general) because this would allow for more helpful feedback to the user if something goes wrong. An example would be enforcing white space between search strings.