-1

I need RegExp expression, which selects each letter once in the sentence (case insensitive). Can you help me?

Input string is:

AaAaaAaaabbacdaasccasddasdascasdasZz

Result must be (in any order):

abcdsz

UPD: ok, i got it. No RegExp solution. Programmatically solution below. (Qt)

TylerH
  • 20,799
  • 66
  • 75
  • 101
warchantua
  • 1,154
  • 1
  • 10
  • 24
  • 4
    I think this should treated programmatically not by regex, no ?! – Gilles Quénot Dec 09 '14 at 00:32
  • 1
    This is pretty impossible in regex, as regexes only will match consecutive characters. Unless you feature an engine that supports to yield all captures of a group that is placed in a lookahead. – Bergi Dec 09 '14 at 00:35
  • Another solution is to find the way programmatically and share ;) Questions/answers are not only for you but also for all future readers – Gilles Quénot Dec 09 '14 at 00:39

2 Answers2

2

It is possible, with two caveats, each letter is in their separate match, and there is no guarantee about the case of the character (the last appearance of the character, uppercase or lowercase regardless, will be picked).

(.)(?!.*\1)

QRegExp implements backreference, and look-ahead, so the regex above should work.

It should be used with Qt::CaseInsensitive option on.

. in QRegExp by default matches any character without exception (which is equivalent to having s option on all the time in Perl, PCRE, Java, etc.), so depending on your requirement, you might want to strip all space characters in the string first..

Demo at regex101 (it uses PCRE engine, but there should be no difference in behavior for this regex)

nhahtdh
  • 55,989
  • 15
  • 126
  • 162
1

Programmatically solution is here:

QString s = "AaAaaAaaabbacdaasccasddasdascasdasZz";
QString variables = "";
for(int i=0;i<s.length(); ++i)
{
        if(s[i].isLetter() && !variables.contains(s[i]))
                variables+=s[i];
}
// variables = "abcdsz"
warchantua
  • 1,154
  • 1
  • 10
  • 24