1

QString has a method remove that takes a QRegExp. It removes every occurrence of the regular expression.

Is there a way to remove only the first occurrence of the regular expression?

Answer to QString replace only first occurrence doesn't help. See comment from Akiva there.

Community
  • 1
  • 1
Thomas Klier
  • 449
  • 4
  • 16
  • 1
    Possible duplicate of [QString replace only first occurrence](http://stackoverflow.com/questions/21024855/qstring-replace-only-first-occurrence) – kfunk Jan 05 '16 at 13:25

1 Answers1

1

You can use next code:

QString message = "This is the original text";
QRegExp rx = QRegExp("is|he", Qt::CaseInsensitive);

if (message.contains(rx)){
    message = message.remove(rx.pos(0), rx.cap(0).size());
}

The final message is:

Th is the original text
kato2
  • 535
  • 4
  • 15