Important: Please read the Qt Code of Conduct -

Help with making regular expression

  • @QString str("AZ ZA 1234 XX");
    QRegExp reg("[A-Z0-9]{2}");
    int pos = reg.indexIn(str);
    while(pos >= 0)
    qDebug() << reg.cap(0);
    pos = reg.indexIn(str, pos+1);

    I'm going to get "AZ", "ZA", "XX", but get "AZ", "ZA", "12", "23", "34", "XX"
    How to fix?

  • @QRegExp reg("(?:^|\s)([A-Z0-9]{2})(?:\s|$)");@
    But I'm getting spaces too, but I don't want

  • Moderators

    You are including digits, but you don't want them, right? Then just remove that part.
    QRegExp reg("[A-Z]{2}");

  • No, I want to get letter+letter or letter+number or number+number.
    So, if I input "12 1234 AA 0 A1 1A ZZ", I want to get "12", "AA", "A1", "1A", "ZZ"

  • Moderators

    Ah, ok. You need to either add space to your regexp, or notify it somehow that you want to parse only whole words (check out the documentation for exact syntax).

    Or, you can split your string using space as separator, and then feed all substrings into the regexp.

Log in to reply