-1

I am trying to build a regular expression in Qt for the following set of strings:

The set can contain all the set of strings of length 1 which does not include r and z.

The set also includes the set of strings of length greater than 1, which start with z, followed by any number of z's but must terminate with a single character that is not r and z

So far I have developed the following:

[a-qs-y]?|z+[a-qs-y]

But it does not work.

TylerH
  • 20,799
  • 66
  • 75
  • 101
Sumeet
  • 8,086
  • 3
  • 25
  • 45

1 Answers1

0

The question mark in your regular expression causes the first alternative to either match lowercase strings of length 1 excluding r and z or the empty string, and as the empty string can be matched within any string, the second alternative will never be matched against. The rest of your regular expression matches your specification, although you will probably want to make your regular expression only match entire strings by anchoring it:

QRegularExpression re("^[a-qs-y]$|^z+[a-qs-y]$");
QRegularExpressionMatch match = re.match("zzza");
if (match.hasMatch()) {
    QString matched = match.captured(0);
    // ...
}
Johannes Riecken
  • 2,301
  • 16
  • 17