[Interest] QRegularExpression

Thiago Macieira thiago.macieira at intel.com
Tue Oct 29 16:20:45 CET 2013


On terça-feira, 29 de outubro de 2013 06:16:04, Bob Hood wrote:
> 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();
>     }

This misses the point of using QRegularExpression...

Also, here's a hint: use the QString::replace overload that takes an index and 
length of the substring to replace.
-- 
Thiago Macieira - thiago.macieira (AT) intel.com
  Software Architect - Intel Open Source Technology Center
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 190 bytes
Desc: This is a digitally signed message part.
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20131029/e97c797c/attachment.sig>


More information about the Interest mailing list