[solved] QRegExp to add spaces every 2nd character ?

  • How the QRegExp should be designed ...

    @ QString pacA = "CC0D00FF6F00A8C0FFFFFFFF0000";
    qDebug() << pacA;
    QString pacB = pacA.replace(QRegExp("."), " " );
    qDebug() << pacB;@

    ... to get: "CC 0D 00 FF 6F 00 A8 C0 FF FF FF FF 00 00"

    pacA is created by reading a QByteArray filled with Byte data from QSerialPort.
    Maybe there's a possibility to add the space directly when reading the QByteArray ?

  • Lifetime Qt Champion

    Hi and welcome to devnet,

    @QString pacB = pacA.replace(QRegExp("([A-Z0-9]{2})"), "\1 " ).simplified();@

    Hope it helps

  • Great, thank you !

    I tried pretty much the same 27 times, always slightly different.
    Still analyzing what i did bad. I think the parenthesis made me mad


  • Lifetime Qt Champion

    You're welcome !

    You have a little regexp tool in Qt's example that helps write/test QRegExp.

    Now that it's working, please update the thread title prepending [solved] so other forum users may know a solution has been found :)

  • I found and tested the example. Nice tool.
    Attaching a screenshot here is only possible if i have
    a place to make it available on the web, right ?

  • Lifetime Qt Champion

    Currently, yes, but that might change in the future (following the thread of our new community manager)

Log in to reply