Is there any way to configure Redis to be case insensitive with regards to Keys?
Asked Answered
M

4

16

So for example:

Foo : Bar

Could also be looked up as FOO, foo, fOO etc?

Messmate answered 15/7, 2011 at 16:12 Comment(1)
And what has this to do with Python?Doublepark
D
24

No. You should lowercase/uppercase all your keys if you want that.

Ducktail answered 15/7, 2011 at 17:29 Comment(0)
C
7

redis keys is case sensitive,my solution is that: key-->Foo:Bar keyword-->f

keys("[fF]*") or keyword-->foo

keys("[fF][oO][oO]*") you have to convert normal string to this format '[Ff][Oo]';

i write a method for this:

public static String toIgnoreCasePattern(String str){
    StringBuilder sb = new StringBuilder();
    char []chars = str.toCharArray();
    char upperCaseC;
    for(char c : chars){
        boolean isLowerCase = Character.isLowerCase(c);
        upperCaseC = isLowerCase ? Character.toUpperCase(c) : c;
        sb.append("[").append(c).append(upperCaseC).append("]");
    }
    return sb.toString();
}

I hope this answer can help you.

Cosby answered 14/5, 2013 at 9:6 Comment(0)
P
2

In C#

public static String toIgnoreCasePattern(String str)
{
    StringBuilder sb = new StringBuilder();
    char[] chars = str.ToCharArray();
    char upperCaseC;
    foreach (var c in chars)
    {
        bool isLowerCase = char.IsLower(c);
        upperCaseC = isLowerCase ? char.ToUpper(c) : c;
        sb.Append("[").Append(c).Append(upperCaseC).Append("]");
    }
    return sb.ToString();
}
Pogonia answered 7/8, 2016 at 18:17 Comment(0)
S
0

in Python (to continue the examples)


def nocase(s):
    key = ''
    for c in s:
        if c.isalpha():
            key += f"[{c.upper()}{c.lower()}]"
        else:
            key += c
    return key

# equivalent to 

nocase = lambda s: ''.join([f"[{c.upper()}{c.lower()}]" if c.isalpha() else c for c in s])

This will work with intermixed wildcards.

>>> print(nocase('test'))
[Tt][Ee][Ss][Tt]

>>> print(nocase('*Tester1*'))
*[Tt][Ee][Ss][Tt][Ee][Rr]1*

Kudos to the original idea

Smirk answered 23/4 at 14:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.