QString:删除第一次出现的正则表达式 [英] QString: Remove first occurance of regular expression
本文介绍了QString:删除第一次出现的正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
QString
具有采用QRegExp
的方法remove
.它将删除每次出现的正则表达式.
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?
回答仅将QString替换为首次出现是没有帮助的.在那里查看Akiva的评论.
Answer to QString replace only first occurrence doesn't help. See comment from Akiva there.
推荐答案
您可以使用下一个代码:
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());
}
最后一条消息是:
Th is the original text
这篇关于QString:删除第一次出现的正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文