IMPORTANT: always use SCAN instead of evil ) KEYS

Matching Redis patterns is somewhat functionally limited (see the stringmatchlen implementation in util.c ) and does not provide what you look for an ATM. We consider the following possible routes:
- Extend
stringmatchlen to fit your requirements, possibly sending it as a PR. - Think about what you are trying to do - selecting a subset of keys will always be ineffective, if you do not index them, try instead to keep track of the names of all non-user keys (i.e., in the Redis set).
- If you really insist on scanning the entire key space and matching against negative patterns, one way to achieve this is with Lua's little magic.
Consider the following dataset and script:
127.0.0.1:6379> dbsize (integer) 0 127.0.0.1:6379> set user:1 1 OK 127.0.0.1:6379> set use:the:force luke OK 127.0.0.1:6379> set non:user a OK
Lua (save this as scanregex.lua ):
local re = ARGV[1] local nt = ARGV[2] local cur = 0 local rep = {} local tmp if not re then re = ".*" end repeat tmp = redis.call("SCAN", cur, "MATCH", "*") cur = tonumber(tmp[1]) if tmp[2] then for k, v in pairs(tmp[2]) do local fi = v:find(re) if (fi and not nt) or (not fi and nt) then rep[
Exit - the first regular match, the second time - addition:
foo@bar :~$ redis-cli --eval scanregex.lua , "^user" 1) "user:1" foo@bar :~$ redis-cli --eval scanregex.lua , "^user" 1 1) "use:the:force" 2) "non:user"
source share