[Interest] QRegularExpression

Bob Hood bhood2 at comcast.net
Tue Oct 29 13:16:04 CET 2013


On 10/29/2013 3:17 AM, Graham Labdon wrote:
> Hi
> Is it possible to use QRegularExpression to perform search and replace on a
> string?
> I know I can use QString::replace and pass a regular expression, but wanted
> to know if it's possible (and how) using just QRegularExpression

Sure:

    QString str(tr("This is a line of text that is a line of text that is a
line of text."));
    QString rpl(tr("bunch of"));
    QRegExp exp("line of");

    int pos = 0;
    while((pos = exp.indexIn(str, pos)) != -1)
    {
        str = str.left(pos) + rpl + str.right(str.length() - (pos +
exp.matchedLength()));
        pos += rpl.length();
    }

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20131029/9a698128/attachment.html>


More information about the Interest mailing list