PDA

View Full Version : simple QRegExp



paolom
9th July 2010, 12:29
if I have:

"My name is @Pinco Pallino@ and your name is @Panco Pallone@..."

I want to extract 'Pinco Pallino' and 'Panco Pallone'

I think that is possible with the regular expression QRegExp ...how can I do this???

P.S. I could have: " @Pinco Pallino@ is my name ... " (it could be n match words between @ )

Thanks

P.S. 2 : I've try with:




QString str ="My name is @Pinco Pallino@ and your name is @Panco Pallone@";
QRegExp regex("@*@");
regex.setPatternSyntax(QRegExp::Wildcard);
regex.indexIn(str);

QStringList list = regex.capturedTexts();


the result is: "@Pinco Pallino@ and your name is @Panco Pallone@"...Instead I want:

@Pinco Pallino@
@Panco Pallone@

mstegehu
9th July 2010, 15:28
Paolom,

Look at:


void QRegExp::setMinimal ( bool minimal )

and with the result you get you might want to look at:


QString QRegExp::cap ( int nth = 0 ) const

Regards,

marcel

paolom
9th July 2010, 18:27
Thanks, this is my final working code ( i hope! )\:




// str contains the string to match

QRegExp regex("@.*@");
regex.setMinimal(true);

QStringList list;
int pos = 0;

while ((pos = regex.indexIn(str, pos)) != -1)
{
list << regex.cap(0).remove(QChar('@'),Qt::CaseInsensitive );
pos += regex.matchedLength();
}

return list;

Lykurg
9th July 2010, 20:29
I would go for a pure regular expression like:
@([^@]+)@
or if you want the @ also:
(@[^@]+@)