QRegExp : search a pattern that does not contain another pattern
-
Hi,
I am trying to search the following pattern :
"<b>.+</b>$"But this pattern must not contain a "<b>" tag.
Given the following example:
abba <b>acba</b> <b>adba</b>If I use the previous pattern, the result is :
<b>acba</b> <b>adba</b>But the good result, which I want is :
<b>adba</b>Because I want disallow a <b> tag between the two <b> and </b> border tags.
So, how can I do that?
I use QRegExp and I code in c++ language.
Thank you in advance for your help,
Floréal.
-
One way would be to change the pattern to "<b>.+</b>" and setMinimal(true).
This would give you 2 results: "<b>acba</b>" and "<b>adba</b>". You can then just take the last one. -
Hi,
Using .+ is a bad idea, essentially you are telling that you want a <b> then everything and anything at list once then a </b> at the end of the line. Looks you are rather looking for <b> at least one word character then </b> end of the line so something like
@<b>\w+</b>$@
Beware this is the real regexp not the one escaped for use with QRegExp.
On a side not you should consider using QRegularExpression if you are using Qt 5
-
Thanks for your quick replies. But...
[quote author="Chris Kawa" date="1418216735"]One way would be to change the pattern to "<b>.+</b>" and setMinimal(true).
This would give you 2 results: "<b>acba</b>" and "<b>adba</b>". You can then just take the last one.
[/quote]I have try it. I use this code:
@QString res = "not found";
QString data = "abba <b>acba</b> <b>adba</b>";
QRegExp exp;
exp.setMinimal(true);
exp.setPattern("<b>.+</b>");
if (data.contains(exp))
{
QStringList list = exp.capturedTexts();
foreach (QString cap, list) {
qDebug() << cap;
}
}@But it give me only one result, and the first, not the last:
"<b>acba</b>"May be something is wrong?
[quote author="SGaist" date="1418217112"]Hi,
Using .+ is a bad idea, essentially you are telling that you want a <b> then everything and anything at list once then a </b> at the end of the line. Looks you are rather looking for <b> at least one word character then </b> end of the line so something like
@<b>\w+</b>$@
Beware this is the real regexp not the one escaped for use with QRegExp.
On a side not you should consider using QRegularExpression if you are using Qt 5[/quote]
I have already tested it, and it works very well. But the string I given here is only an example. In reality, the content between <b> and </b> might not be alphanumerical characters. For example, we can have :
abcd <b>acba</b> <b>ad i>2 ba</b>where we want to find :
<b>ad i>2 ba</b>And here, the solution you give does not work anymore...
-
Then use a more specific regexp like
@<b>[\w\s>]+</b>$@
Complete it with the special chars you might get
-
bq. But it give me only one result, and the first, not the last
capturedTexts() returns a list strings captured in a capture clause ( and ). You don't have such. The first element in that list is always the entire matched string so its of no use to you. Either surround the part that you want to capture with () or iterate over matches using indexIn().
Or go with the suggestion SGaist gave.