Workaround for finding round brackets - "(" and ")"

Quite recently, Anki drove me crazy over trying to find round brackets. Then I found a thread of earlier this year Cannot search for round brackets (closed, cannot comment there) - just to find out that this problem hasn’t been solved for over 5 years now.

However, here’s a workaround (to prevent others from going crazy):

Instead of trying deck:current ( or deck:current "(" you can use the regular expression escape sequence for hex character codes \x. Round brackets have the ASCII character codes \x28 and \x29, respectively. Thus

deck:current re:\x28

will do the trick. Or, limited to a field: deck:current myfield:re:\x28. Of course you’re dealing with regexes now. If your search term contains special regex characters, you’ll get unexpected results.

PS: Neither using a character class [(] or trying to escape the bracket \( did work. It seems that all expressions with unmatched brackets fail. In my opinion a working parser for search terms is essential and should be fixed before implementing some other fancy feature.

PS2: Damien Elmes let me know that "(" should work now. I tried that, but it only works with deck:current "(". If limited to a field deck:current myfield:"(" it still doesn’t work.

myfield:"(" does work, but

[…] searching on fields requires an ‘exact match’ by default.

You can use myfield:"*(*" instead.

I’m currently working on overhauling the search syntax. Since what you’re describing isn’t a bug, it won’t be changed, but if all goes well you’ll also have the option to escape parentheses with a \ at some point.

However, if you find any other issue with the search syntax, please let me know.

1 Like

Ah, I did not know about the exact match restriction in field searches. Good to know.

But not being able to escape brackets or search for brackets in regular expressions very much seems like a bug to me that begs for being fixed :upside_down_face:

You can by using double quotes, can’t you?
However, by now my revision of the parser has been merged and in future releases, you’ll only be forced to use quotes if you want to match whitespace.

Edit: I have just realised that there is another case where quotes will be compulsory, namely when using unescaped parentheses in a RegEx (which is possible because Anki interprets it as raw input).

Sorry, I could not find any regex that finds entries ending with a round bracket, for example.

I tried things like deck:current myfield:re:".*\)" or deck:current myfield:re:".*[)]" - none worked. The regex documentation doesn’t even mention how to escape special characters and the Anki manual is generally rather vague about the search syntax (especially about the handling of double quotes).

I do not have the time to do further bug searching, I’m afraid. Hoping future releases will bring relief.

Here, the problem is that the quotes have to enclose either the entire term or the part after the first colon. Both "myfield:re:.*\)" and myfield:"re:.*\)" will work. Maybe this case where there are two colons should be made more clear in the manual.

I totally agree about the lack of documentation concerning escape sequences. I’ve already made a PR which covers escaping in all cases and for every special character. It describes the behaviour of the new parser, though. I guess it won’t be merged before the next stable release.
But it’s there on GitHub for everyone building from code or using the following betas.


Edit: It's already been merged and can be found in the [manual](https://docs.ankiweb.net/#/searching?id=matching-special-characters) now.
1 Like