PDA

View Full Version : Problem with regular expression not working



Suppaman
23rd March 2013, 11:53
Hi all

I'm trying to find a working regular expression for make this operation:

I have a text formatted as below:

{text1}{text2}{text3}{text4}.....{textN}

I need a regular expression for parse this text and "extract" a list of strings inside the {}. So the result should be a list like:

text1
text2
text3
text4
...
textN

I tried using the following code:


QRegExp RegExp("\\{(.*?)\\}");
QStringList List;
int Pos;

Pos = RegExp.indexIn(MyText);
List = RegExp.capturedTexts();

but doesn't work, capturedTexts() return me a list with only two empty lines. I tested the regular expression \\{(.*?)\\} in a simulator and it work as expected but it seem QT doesn't accept it.

Someone can suggest me the right regular expression?

Thank you

mattc
23rd March 2013, 20:23
The old QRegExp class does not support the non-greedy operator .*?, you need to call QRegExp::setMinimal(true) for that. Also, you are supposed to iterate over the captured texts:


QRegExp r("\\{(.*)\\}");
r.setMinimal(true);

int pos = 0;

while ((pos = r.indexIn(myText, pos)) != -1)
{
qDebug() << r.cap(1);
pos += r.matchedLength();
}
If you are working with Qt 5, you can use the new QRegularExpression:


QRegularExpression r("\\{(.*?)\\}");
QRegularExpressionMatchIterator i = r.globalMatch(myText);

while (i.hasNext())
{
QRegularExpressionMatch match = i.next();
qDebug() << match.captured(1);
}

wysota
24th March 2013, 14:50
This should work too:


QRegExp rx("\\{([\\}]+)\\}");

amleto
24th March 2013, 15:53
http://imgs.xkcd.com/comics/perl_problems.png

Suppaman
24th March 2013, 17:54
The old QRegExp class does not support the non-greedy operator .*?, you need to call QRegExp::setMinimal(true) for that. Also, you are supposed to iterate over the captured texts:


QRegExp r("\\{(.*)\\}");
r.setMinimal(true);

int pos = 0;

while ((pos = r.indexIn(myText, pos)) != -1)
{
qDebug() << r.cap(1);
pos += r.matchedLength();
}


This solution work very well, thank you so much for the help. :D

I need to read the QRegExp documentation again since I didn't note the "unsupported" operator. I suppose is my fault since I'm not expert in regular expression...

Anyway thank you again to all

Suppaman
9th April 2013, 20:51
Sorry, I need to ask help again for a regular expression. Unfortunately regular expression still give me the headache. :(

I have an example text like the following string:

achuweshurdddjhukk

I want to replace using the call of QString all the occurence of the substring "hu" that doesn't start with 'c' or 's'. The regular expression "[^cs]hu" is able to make this job but my problem is that this expression extract the "complete" string, in this example "jhu". On the contrary I need only to have "hu" without the "discrimination" letter. This because using the replace function:

QString Text("achuweshurdddjhukk");

Text.replace(QRegExp("[^cs]hu"), "XX");

I'll have as result:

"achuweshurdddXXkk"

and not

"achuweshurdddjXXkk"

as I need. In the "wrong" case also the 'j' letter near the matched "hu" is replaced.

Someone can help me fixing thie regular expression?

Thank you for the patience

amleto
9th April 2013, 23:40
looks like you will have to work your way through the string and replace matches as you find them. What you want is a 'look behind' regex, which I don't see in Qt 4.x