In Redis, keys user*
will print all keys starting with user
.
For example:
keys user*
1) "user2"
2) "user1"
Now, I want all keys that don't start with user
to be printed.
How could I do that?
In Redis, keys user*
will print all keys starting with user
.
For example:
keys user*
1) "user2"
2) "user1"
Now, I want all keys that don't start with user
to be printed.
How could I do that?
According to redis keys documentation the command supports glob style patterns, not regular expressions.
and if you look at the documentation, you'll see that the "!" character is not special as opposites to regular expressions.
Here is a simple test I ran in my own db:
So I just don't think what you are trying to achieve is possible via the keys command.
Besides, the keys command is not very suitable for production environment as it locks your whole redis database.
I would recommend getting all the keys with the scan command, store them locally, and then remove them using LUA
@Karthikeyan Gopall you nailed it in your comment above and this saved me a bunch of time. Thanks!
Here's how you can use it in various combinations to get what you want:
IMPORTANT: always use
SCAN
instead of (the evil)KEYS
Redis' pattern matching is somewhat functionally limited (see the implementation of
stringmatchlen
in util.c) and does not provide that which you seek ATM. That said, consider the following possible routes:stringmatchlen
to match your requirements, possibly submitting it as a PR.Consider the following dataset and script:
Lua (save this as
scanregex.lua
):Output - first time regular matching, 2nd time the complement: