8

I have a text form:

Last Name:SomeName, Day:23 ...etc

From Last Name:SomeName, I would like to get Last Name, and separately SomeName.

I have tried to use QRegularExpression,

QRegularExpression re("(?<label>\\w+):(?<text>\\w+)");

But I am getting the result:

QString label = match.captured("label") //it gives me only Name

What I want is whatever text till ":" to be label, and after to be text.

Any ideas?

Noam M
  • 3,156
  • 5
  • 26
  • 41
amol01
  • 1,823
  • 4
  • 21
  • 35
  • Related: [How can I partition a QByteArray efficiently?](https://stackoverflow.com/questions/5978124/how-can-i-partition-a-qbytearray-efficiently) – iammilind Oct 21 '19 at 14:22

1 Answers1

10

You could use two different methods for this, based on your need:

main.cpp

#include <QString>
#include <QDebug>

int main()
{
    QString myString = "Last Name:SomeName, Day:23";
    QStringList myStringList = myString.split(',').first().split(':');
    qDebug() << myStringList.first() << myStringList.last();
    return 0;
}

main.pro

TEMPLATE = app
TARGET = main
QT = core
SOURCES += main.cpp

Build and Run

qmake && (n)make

Output

"Last Name" "SomeName"
BaCaRoZzo
  • 7,502
  • 6
  • 51
  • 82
László Papp
  • 51,870
  • 39
  • 111
  • 135