QString: Remove first occurance of regular expression

生来就可爱ヽ(ⅴ<●) 提交于 2020-01-16 03:58:47

问题


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.


回答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


来源:https://stackoverflow.com/questions/34612750/qstring-remove-first-occurance-of-regular-expression

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!